Android ViewFlipper 不翻转

发布于 2024-10-30 20:10:51 字数 3262 浏览 1 评论 0原文

我有一个 ViewFlipper 应该对 fling 手势做出反应,但它没有。

的 ViewFlipper 的Activity

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    listView = this.getListView();
    detector = new GestureDetector(this, new FlingGestureListener(listView));
    ...
}

FlingGestureListener

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    int pos = source.pointToPosition(Math.round(e1.getX()), Math.round(e1.getY()));
    View v = source.getChildAt(pos - source.getFirstVisiblePosition());
    System.out.println("fling: x=" + velocityX + " y=" + velocityY);
    try {
        IFlingable flingable = (IFlingable) v;
        if(velocityY > -200 && velocityY < 200) {
            if(velocityX < 0)
                flingable.fling(IFlingable.RIGHT_TO_LEFT);
            else if(velocityX > 0)
                flingable.fling(IFlingable.LEFT_TO_RIGHT);
        }
    } catch(Exception e) {}
    return false;
}

带有实现 IFlingable

public void fling(int direction) {
    System.out.println("flip: " + direction);
    switch(direction) {
        case IFlingable.LEFT_TO_RIGHT:
            System.out.println("piep");
            GUIHelper.setAnimationSlideLeftToRight(context, switcher);
            switcher.showNext();
            break;
        case IFlingable.RIGHT_TO_LEFT:
            System.out.println("pup");
            GUIHelper.setAnimationSlideRightToLeft(context, switcher);
            switcher.showPrevious();
            break;
    }
}

布局

<ViewFlipper android:id="@+id/viewSwitcher"
    android:layout_height="wrap_content"
    android:layout_width="match_parent" android:layout_weight="1"     
    android:inAnimation="@anim/slide_in_left" 
    android:outAnimation="@anim/slide_out_right">
        <LinearLayout android:layout_height="wrap_content" 
           android:layout_width="match_parent" android:orientation="vertical">
             ...
        </LinearLayout>
      ...
    </ViewFlipper>

日志

fling: x=2542.3613 y=95.877945
flip: 0
piep

View我得到了正确的日志消息,因此 ViewFlipper 上的 showNext() 被执行,但它不会更改其在 gui 上的视图。我错过了什么吗?我有另一种布局,使用 ViewSwitcher 而不是 Flipper,并且该布局有效。

编辑:

以下是缺少的课程:

public class GUIHelper {
... 

public static void setAnimationSlideLeftToRight(Context context, ViewAnimator switcher) {
    Animation in = AnimationUtils.loadAnimation(context, R.anim.slide_in_left);
    Animation out = AnimationUtils.loadAnimation(context, R.anim.slide_out_right);
    switcher.setInAnimation(in);
    switcher.setOutAnimation(out);
}

public static void setAnimationSlideRightToLeft(Context context, ViewAnimator switcher) {
    Animation in = AnimationUtils.loadAnimation(context, R.anim.slide_in_right);
    Animation out = AnimationUtils.loadAnimation(context, R.anim.slide_out_left);
    switcher.setInAnimation(in);
    switcher.setOutAnimation(out);
}

...
}

public interface IFlingable {

    public static final int LEFT_TO_RIGHT = 0;
    public static final int RIGHT_TO_LEFT = 1;

    public void fling(int direction, boolean fling);

}

I have a ViewFlipper which should react to a fling gesture, but it does not.

Activity

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    listView = this.getListView();
    detector = new GestureDetector(this, new FlingGestureListener(listView));
    ...
}

FlingGestureListener

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    int pos = source.pointToPosition(Math.round(e1.getX()), Math.round(e1.getY()));
    View v = source.getChildAt(pos - source.getFirstVisiblePosition());
    System.out.println("fling: x=" + velocityX + " y=" + velocityY);
    try {
        IFlingable flingable = (IFlingable) v;
        if(velocityY > -200 && velocityY < 200) {
            if(velocityX < 0)
                flingable.fling(IFlingable.RIGHT_TO_LEFT);
            else if(velocityX > 0)
                flingable.fling(IFlingable.LEFT_TO_RIGHT);
        }
    } catch(Exception e) {}
    return false;
}

View with ViewFlipper which implements IFlingable

public void fling(int direction) {
    System.out.println("flip: " + direction);
    switch(direction) {
        case IFlingable.LEFT_TO_RIGHT:
            System.out.println("piep");
            GUIHelper.setAnimationSlideLeftToRight(context, switcher);
            switcher.showNext();
            break;
        case IFlingable.RIGHT_TO_LEFT:
            System.out.println("pup");
            GUIHelper.setAnimationSlideRightToLeft(context, switcher);
            switcher.showPrevious();
            break;
    }
}

Layout

<ViewFlipper android:id="@+id/viewSwitcher"
    android:layout_height="wrap_content"
    android:layout_width="match_parent" android:layout_weight="1"     
    android:inAnimation="@anim/slide_in_left" 
    android:outAnimation="@anim/slide_out_right">
        <LinearLayout android:layout_height="wrap_content" 
           android:layout_width="match_parent" android:orientation="vertical">
             ...
        </LinearLayout>
      ...
    </ViewFlipper>

Log

fling: x=2542.3613 y=95.877945
flip: 0
piep

I get the right log messages so the showNext() on the ViewFlipper gets executed but it does not change its view on the gui. Am I missing something? I have another layout with a ViewSwitcher instead of the Flipper and that one works.

EDIT:

Here are the missing classes:

public class GUIHelper {
... 

public static void setAnimationSlideLeftToRight(Context context, ViewAnimator switcher) {
    Animation in = AnimationUtils.loadAnimation(context, R.anim.slide_in_left);
    Animation out = AnimationUtils.loadAnimation(context, R.anim.slide_out_right);
    switcher.setInAnimation(in);
    switcher.setOutAnimation(out);
}

public static void setAnimationSlideRightToLeft(Context context, ViewAnimator switcher) {
    Animation in = AnimationUtils.loadAnimation(context, R.anim.slide_in_right);
    Animation out = AnimationUtils.loadAnimation(context, R.anim.slide_out_left);
    switcher.setInAnimation(in);
    switcher.setOutAnimation(out);
}

...
}

public interface IFlingable {

    public static final int LEFT_TO_RIGHT = 0;
    public static final int RIGHT_TO_LEFT = 1;

    public void fling(int direction, boolean fling);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国产ˉ祖宗 2024-11-06 20:10:51

你好,这是 listView 上正在运行的滑动代码,就像你的 xml 一样,我在 ViewFlipper 中放置了一个带有父 LinearLayout 的 ListView

import android.app.Activity;
import android.os.Bundle;

import android.view.GestureDetector;

import android.view.GestureDetector.SimpleOnGestureListener;

import android.view.MotionEvent;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import android.widget.AdapterView.OnItemClickListener;


public class MainActivity extends Activity {

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;
    private Animation slideLeftIn;
    private Animation slideLeftOut;
    private Animation slideRightIn;
    private Animation slideRightOut;
    private ViewFlipper viewFlipper;
    private ListView lv;
    private String[] city = { "Indore", "Bhopal", "Khargone", "Ujjain",
        "Nasik", "Pune", "Delhi", "Mumbai", "Noida", "Hyderabad",
        "Banglore", "Ajmer", "Goa", "Jaipur", "Nagpur", "" };
    private String[] country = { "India", "Bhutan", "Kuwait", "USA", };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        lv = (ListView) findViewById(R.id.List01);
        ListView lv2 = (ListView) findViewById(R.id.List02);

        viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils
                .loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils
                .loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this,
                R.anim.slide_right_out);

        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
        lv.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, city));
        lv2.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, country));
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView arg0, View view, int position,
                    long id) {
                // user clicked a list item, make it "selected"
                Toast.makeText(getBaseContext(), "Item Clicked",
                        Toast.LENGTH_SHORT).show();
                // selectedAdapter.setSelectedPosition(position);
            }
        });
        lv2.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView arg0, View view, int position,
                    long id) {
                // user clicked a list item, make it "selected"
                Toast.makeText(getBaseContext(), "Item List2 Clicked",
                        Toast.LENGTH_SHORT).show();
                // selectedAdapter.setSelectedPosition(position);
            }
        });
    }

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    viewFlipper.setInAnimation(slideLeftIn);
                    viewFlipper.setOutAnimation(slideLeftOut);
                    viewFlipper.showNext();
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    viewFlipper.setInAnimation(slideRightIn);
                    viewFlipper.setOutAnimation(slideRightOut);
                    viewFlipper.showPrevious();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
            return true;
        else
            return false;
    }
}

包括所使用的布局(main_layout)......

 <?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Do not Click Just swipe on remaining area" />
    </LinearLayout>

    <ViewFlipper
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/flipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/ViewFlipper01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/List01"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </ListView>
        </LinearLayout>

Hello this is the running swipe code on listView just as your xml I have placed a ListView with parent LinearLayout in ViewFlipper

import android.app.Activity;
import android.os.Bundle;

import android.view.GestureDetector;

import android.view.GestureDetector.SimpleOnGestureListener;

import android.view.MotionEvent;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import android.widget.AdapterView.OnItemClickListener;


public class MainActivity extends Activity {

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;
    private Animation slideLeftIn;
    private Animation slideLeftOut;
    private Animation slideRightIn;
    private Animation slideRightOut;
    private ViewFlipper viewFlipper;
    private ListView lv;
    private String[] city = { "Indore", "Bhopal", "Khargone", "Ujjain",
        "Nasik", "Pune", "Delhi", "Mumbai", "Noida", "Hyderabad",
        "Banglore", "Ajmer", "Goa", "Jaipur", "Nagpur", "" };
    private String[] country = { "India", "Bhutan", "Kuwait", "USA", };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        lv = (ListView) findViewById(R.id.List01);
        ListView lv2 = (ListView) findViewById(R.id.List02);

        viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils
                .loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils
                .loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this,
                R.anim.slide_right_out);

        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
        lv.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, city));
        lv2.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, country));
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView arg0, View view, int position,
                    long id) {
                // user clicked a list item, make it "selected"
                Toast.makeText(getBaseContext(), "Item Clicked",
                        Toast.LENGTH_SHORT).show();
                // selectedAdapter.setSelectedPosition(position);
            }
        });
        lv2.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView arg0, View view, int position,
                    long id) {
                // user clicked a list item, make it "selected"
                Toast.makeText(getBaseContext(), "Item List2 Clicked",
                        Toast.LENGTH_SHORT).show();
                // selectedAdapter.setSelectedPosition(position);
            }
        });
    }

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    viewFlipper.setInAnimation(slideLeftIn);
                    viewFlipper.setOutAnimation(slideLeftOut);
                    viewFlipper.showNext();
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    viewFlipper.setInAnimation(slideRightIn);
                    viewFlipper.setOutAnimation(slideRightOut);
                    viewFlipper.showPrevious();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
            return true;
        else
            return false;
    }
}

Included the layout(main_layout) that is used.....

 <?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Do not Click Just swipe on remaining area" />
    </LinearLayout>

    <ViewFlipper
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/flipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/ViewFlipper01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/List01"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </ListView>
        </LinearLayout>
清浅ˋ旧时光 2024-11-06 20:10:51

这是我使用的xml代码。

  <ViewFlipper 
    android:id="@+id/vf"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

    <ListView
        android:id="@+id/list1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ></ListView>
    </LinearLayout>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    <ListView
        android:id="@+id/list2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ></ListView>
    </LinearLayout>
</ViewFlipper>

覆盖dispatchTouchEvent而不是onTouchEvent

@Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        super.dispatchTouchEvent(event);
        return gestureDetector.onTouchEvent(event);
    }

这工作正常:)滚动视图和翻转器都工作正常。

This is my xml code used.

  <ViewFlipper 
    android:id="@+id/vf"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

    <ListView
        android:id="@+id/list1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ></ListView>
    </LinearLayout>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    <ListView
        android:id="@+id/list2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ></ListView>
    </LinearLayout>
</ViewFlipper>

Override dispatchTouchEvent instead of onTouchEvent

@Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        super.dispatchTouchEvent(event);
        return gestureDetector.onTouchEvent(event);
    }

This works fine :) Scroll View and flipper both works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文