在多个视图上设置onTouchListeners

时间:2016-05-16 08:48:11

标签: android

我在LinearLayout创建了3个视图,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:background="#FFFFFF"
    tools:context=".MainActivity" >
    <View
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:id="@+id/left_scroll">
    </View>
    <View
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight=".3"
        android:id="@+id/center_scroll">
    </View>
    <View
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:id="@+id/right_scroll">
    </View>
</LinearLayout>

现在我为这3个视图创建了3个GestureDetector并实现了他们的方法:

class CenterGestureDetector implements GestureDetector.OnGestureListener
class RightGestureDetector implements GestureDetector.OnGestureListener
class LeftGestureDetector implements GestureDetector.OnGestureListener

我正在尝试为这三者中的每一个做这样的事情:

rightView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //What should go here to hook it up to the gesture detector class?

                return false;
            }
        });
//Similarly for other views.

在我的onCreate方法中,我创建了以下对象:

    CenterGestureDetector centerGestureDetector =  new CenterGestureDetector();
    gestureDetectorCenter = new GestureDetector(this, centerGestureDetector);

    LeftGestureDetector leftGestureDetector = new LeftGestureDetector();
    gestureDetectorLeft = new GestureDetector(this, leftGestureDetector);

    RightGestureDetector rightGestureDetector = new RightGestureDetector();
    gestureDetectorRight = new GestureDetector(this, rightGestureDetector);

0 个答案:

没有答案