在特定视图上检测Android手势(向上/向下滑动)

28

我正试图在Android中实现OnGestureListener。
我的布局中有三个TextViews。
我想要实现的是为其中两个TextView设置手势监听器。
这是布局-

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlMain"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/tvOne"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:text="One" />

    <TextView
        android:id="@+id/tvTwo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvOne"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:text="Two" />

    <TextView
        android:id="@+id/tvThree"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvTwo"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:text="Three" />

</RelativeLayout>

这是活动的内容。

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.GestureDetector;
    import android.view.GestureDetector.OnGestureListener;
    import android.view.MotionEvent;

    public class TimeActivity extends Activity implements OnGestureListener {

    GestureDetector gestureScanner;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wheelview);
        gestureScanner = new GestureDetector(this);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        return gestureScanner.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        Log.i("Test", "On Fling");
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
}

目前所有三个文本视图的飞动都被调用。
有没有办法为布局中的某些特定视图设置手势侦听器?
非常感谢任何帮助。

3个回答

29

在你的onCreate方法中执行此操作。

findViewById(R.id.tvOne).setOnTouchListener(new View.OnTouchListener() { 
            @Override
           public boolean onTouch(View v, MotionEvent event){
                return gestureScanner.onTouchEvent(event);
           }
  });

3
谢谢回答。两个回答都是正确的。我不能接受这两个回答,但我会给你们俩点赞。 - Anukool
如何为特定布局(相对布局)设置onLongPress。 - reegan29
我们如何知道哪个视图被抛出了? - Srujan Barai
哦,是的,那很明显,但是 onFling 方法没有 view 参数。 - Srujan Barai
我该如何使用GestureScanner来使用ACTION_MOVE? - vishal patel
显示剩余2条评论

10
你可以为单个 TextView 设置 OnTouchListener。
findViewById(R.id.tvOne).setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View view, MotionEvent event) {
         // Your code here
    }
}

2

建议

如果你不想检测所有的手势,可以试试我自己创建的SimpleGestureListener类。

现在这里提供一些关于如何使用这个类的代码片段。

用法

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private GestureDetector mDetector;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SimpleGestureListener simpleGestureListener = new SimpleGestureListener();
        simpleGestureListener.setListener(new SimpleGestureListener.Listener() {
            @Override
            public void onScrollHorizontal(float dx) {
                Log.i(TAG,"horizontal = " +dx);
            }

            @Override
            public void onScrollVertical(float dy) {
                Log.i(TAG,"vertical = " +dy);
            }
        });
        mDetector = new GestureDetector(this, simpleGestureListener);
    }

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

它可以用于检测滑动手势:
  • 向左或向右
  • 向上或向下

1
请在您的 Github 项目中将接口 "Listener" 设为公共,谢谢。 - Umair Khalid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接