我有一个viewpager,我想拦截其父容器上的触摸事件。 xml看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="@+id/layoutContainer"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
</RelativeLayout>
因为如果尝试将onTouchListener附加到具有id&#34; layoutContainer&#34;的容器,则视图寻呼机会占用整个屏幕。什么都不会发生。
我希望能够在屏幕上检测垂直滑动手势
private class MyGesture extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return super.onSingleTapConfirmed(e);
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Snap snap = snaps.get(targetPosition);
return super.onSingleTapUp(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float deltaX = e2.getX() - e1.getX();
float deltaY = e2.getY() - e1.getY();
if ((Math.abs(deltaX) < SWIPE_MIN_DISTANCE) || (Math.abs(velocityX) < SWIPE_THRESHOLD_VELOCITY)) {
}
else {
}
if ((Math.abs(deltaY) < SWIPE_MIN_DISTANCE) || (Math.abs(velocityY) < SWIPE_THRESHOLD_VELOCITY)) {
}
else {
app.log("swipe up");
//execute some code here
}
return true;
}
}
我尝试做的另一件事是将触摸侦听器附加到视图寻呼机,然后手动将动作事件传递给手势检测器,但这也不起作用。
答案 0 :(得分:0)
您可以创建一个containerview扩展RelativeLayout,并覆盖此视图组中的onInterceptTouchEvent。 像这样:
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownX = event.getX();
mDownY = event.getY();
return false;
case MotionEvent.ACTION_MOVE:
if (Math.abs(event.getX() - mDownX) < Math.abs(event.getY() - mDownY)) { // Up and down around sliding distance is greater than sliding distance;
return true;
}
return false;
default:
break;
}
return false;
}