在viewFlipper中滑动不起作用

时间:2014-08-05 12:20:14

标签: android viewflipper swipe-gesture

我在浏览列表项时使用了视图鳍状肢显示按钮。但是每当我滑动列表项时都没有发生任何事情。

代码

public class CustomList extends BaseAdapter implements View.OnTouchListener {
    private Context context;

    public Activity activity;
    String[] a;

    public CustomList(Context context, String[] a) {
        this.context = context;
        this.a = a;
    }

    @Override
    public int getCount() {
        return a.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final Viewholder holder;
        if (view == null) {
            holder = new Viewholder();
            view = LayoutInflater.from(context).inflate(R.layout.activity_my, viewGroup, false);
            holder.flipper = (ViewFlipper) view.findViewById(R.id.flipper);
            view.setTag(holder);
        } else {
            holder = (Viewholder) view.getTag();
        }

//        
        return view;
    }


    GestureDetector.SimpleOnGestureListener simpleOnGestureListener
            = new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                               float velocityY) {
            Viewholder holder = new Viewholder();

            float sensitvity = 50;
            if ((e1.getX() - e2.getX()) > sensitvity) {
                holder.flipper.showNext();
            } else if ((e2.getX() - e1.getX()) > sensitvity) {
                holder.flipper.showPrevious();
            }

            return true;
        }

    };


    GestureDetector gestureDetector
            = new GestureDetector(simpleOnGestureListener);

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return gestureDetector.onTouchEvent(motionEvent);
    }

    private class Viewholder {
        ViewFlipper flipper;
        float startX, endX;
    }
}

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ViewFlipper
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/flipper">

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/textView"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="20dp" />

        </LinearLayout>


        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />
        </RelativeLayout>
    </ViewFlipper>

</LinearLayout>
为什么滑动不起作用? 另外我想只有半个列表项应该被翻转不完整

1 个答案:

答案 0 :(得分:0)

您无法通过ViewFlipper进行滑动。它只是一个允许在其中包含的许多视图之间切换的容器。

如果你真的想使用滑动手势,那么你应该使用ViewPager,并创建你的PagerAdapter。