Android viewflipper无法使用抽屉

时间:2016-01-08 10:56:16

标签: android viewflipper drawerlayout

我正在尝试将viewflipper集成到已经有导航抽屉的活动中,但是当导航抽屉正常工作时,viewflipper无法正常工作。主题是刷布局和显示数据。以下是我的代码 XML:

echo 3;

和java代码

<?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>

1 个答案:

答案 0 :(得分:1)

尝试覆盖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);
    }

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

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