-->

Android的 - 手势检测对特定视图(轻扫上/下)(Android - Gesture Dete

2019-07-20 01:48发布

我想实现在Android中OnGestureListener。
我在我的布局3个TextViews。
我试图做到的,是设定手势侦听器两个textViews的。
这里是布局 -

<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" />

这里是活动 -

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;
}

}

在所有这三个textviews目前一扔获取调用。
有没有什么办法,通过它我可以设置手势监听器在布局一些特定的观点
任何帮助将得到高度赞赏。

Answer 1:

在你做这个onCreate方法。

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


Answer 2:

您可以设置OnTouchListener s到个人TextView秒。

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

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


Answer 3:

一条建议

如果你不想检测所有的手势,试试这个类: 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);
    }
}

它可用于检测滑动手势:

  • 左还是右
  • 上或下


文章来源: Android - Gesture Detection (Swipe up/down) on particular view