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

Anu*_*ool 24 android gesture-recognition swipe onfling

我正在尝试在Android中实现OnGestureListener.
我的布局中有三个TextView.
我想要实现的是为两个textView设置Gesture Listener.
这是布局 -

<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" />
Run Code Online (Sandbox Code Playgroud)

这是活动 -

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;
}
Run Code Online (Sandbox Code Playgroud)

}

目前正在调用所有三个文本视图.
有什么方法可以为布局中的某些特定视图设置Gesture Listener.
任何帮助将受到高度赞赏.

M-W*_*eEh 27

在您的onCreate方法中执行此操作.

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

  • 谢谢你的回答.这两个答案都是正确的.我无法接受这两个答案,我两个都在投票给你. (2认同)

jai*_*rik 10

您可以将OnTouchListeners 设置为个人TextViews.

findViewById(R.id.tvOne).setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View view, MotionEvent event) {
         // Your code here
    }
}
Run Code Online (Sandbox Code Playgroud)