Android viewflipper 无法与抽屉一起使用

Android viewflipper not working with drawer

我正在尝试将 viewflipper 集成到已经有导航抽屉的 activity 中,但是 viewflipper 不工作,尽管导航抽屉可以工作。主题是滑动布局并显示数据。下面是我的代码 xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000"
        tools:context="com.inabia.dailyayat.Activities.MainActivity">

        <ViewFlipper
            android:id="@+id/viewflipper"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="6dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/txtayat"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:gravity="center"
                    android:shadowColor="#ffffff"
                    android:shadowDx="3"
                    android:shadowDy="-3"
                    android:shadowRadius="1.5"
                    android:textColor="#ffd700"
                    android:textSize="40sp"
                    android:textStyle="bold" />

            </LinearLayout>

        </ViewFlipper>

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#d3d3d3"
        android:cacheColorHint="#00000000"
        android:choiceMode="singleChoice"
        android:divider="#ffffff"
        android:dividerHeight="1px" />

</android.support.v4.widget.DrawerLayout>

和java代码

private ViewFlipper viewFlipper;
 private float lastX;
viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);

    public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction()) {

            case MotionEvent.ACTION_DOWN:
                lastX = touchevent.getX();
                break;
            case MotionEvent.ACTION_UP:
                float currentX = touchevent.getX();

                // Handling left to right screen swap.
                if (lastX < currentX) {

                    // If there aren't any other children, just break.
                    if (viewFlipper.getDisplayedChild() == 1)
                        break;

                   // c.add(Calendar.DAY_OF_MONTH,-1);
                   // formattedDate = df.format(c.getTime());
                   // txt1.setText(formattedDate);

                    // Next screen comes in from left.
                    viewFlipper.setInAnimation(this, R.anim.slide_in_from_left);
                    // Current screen goes out from right.
                    viewFlipper.setOutAnimation(this, R.anim.slide_out_to_right);

                    // Display next screen.
                    viewFlipper.showNext();
                }

                // Handling right to left screen swap.
                if (lastX > currentX) {

                    // If there is a child (to the left), kust break.
                    if (viewFlipper.getDisplayedChild() == 1)
                        break;

                  //  c.add(Calendar.DAY_OF_MONTH,1);
                  //  formattedDate = df.format(c.getTime());
                  //  txt1.setText(formattedDate);
                    // Next screen comes in from right.
                    viewFlipper.setInAnimation(this, R.anim.slide_in_from_right);
                    // Current screen goes out from left.
                    viewFlipper.setOutAnimation(this, R.anim.slide_out_to_left);

                    // Display previous screen.
                    viewFlipper.showPrevious();
                }
                break;
        }
        return false;
    }

尝试覆盖 dispatchTouchEvent 而不是 onTouchEvent 并使用 GestureDetector.SimpleOnGestureListener 检测设备屏幕上的所有触摸事件希望它对你有用。

代码:

CustomGestureDetector.java

class CustomGestureDetector extends GestureDetector.SimpleOnGestureListener {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                 // Swipe left (next)
                if (e1.getX() > e2.getX()) {
                    viewFlipper.setInAnimation(context, R.anim.left_in);
                    viewFlipper.setOutAnimation(context, R.anim.left_out);

                    viewFlipper.showNext();
                }

                // Swipe right (previous)
                if (e1.getX() < e2.getX()) {
                    viewFlipper.setInAnimation(context, R.anim.right_in);
                    viewFlipper.setOutAnimation(context, R.anim.right_out);

                    viewFlipper.showPrevious();
                }

                return super.onFling(e1, e2, velocityX, velocityY);
            }
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                Log.d("Tap", "Double tap");
            return super.onDoubleTap(e);
            }
            @Override
            public boolean onSingleTapUp(MotionEvent e) {

             return false;
            }
        }

dispatchTouchEvent :

 @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
         mGestureDetector.onTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }

并在 activity 的 onCreate 方法中调用 GestureDetector,如下所示:

CustomGestureDetector customGestureDetector = new CustomGestureDetector();
    mGestureDetector = new GestureDetector(this, customGestureDetector);