onFling向上滑动也会触发向左滑动或向右滑动

时间:2019-02-07 12:26:44

标签: android swipe onfling

在我的Android应用中,我使用的是{strike> name = ['Mark', 'Smits', 'Roger'] age = [23, 45, 32] gender = ['male','male'] item_count_max = max(len(name), len(age), len(gender)) name = name + [None, ] * (item_count_max - len(name)) age = age + [None, ] * (item_count_max - len(age)) gender = gender + [None, ] * (item_count_max - len(gender)) import json for n, a, g in zip(name, age, gender): data = { 'resourceType': 'MyData', 'name': n, 'age': a, 'gender': g } items = data.items() for k, v in items: # We remove empty keys here if not v: data.pop(k) print(json.dumps(data, indent=4)) onFling()的{​​{1}}方法来检测向左,向上,向右,向下滑动。

问题是,当我向上滑动时,也会触发向左或向右滑动。

我尝试使用onTouchListener的值,但是即使我将其从SimpleOnGestureListener增加到SWIPE_THRESHOLD也没什么不同。

当我滑动顶部时,如何确保仅向上滑动。

5000

编辑:我要实现的是,当用户向上滑动然后向左滑动或向右滑动时,不应该触发/调用。(当我向上滑动时,它们现在被调用)

编辑2:我已将50方法更改为仅使用速度,并且在调试后似乎可以正常工作,即当我{{1}时它不会触发public class OnSwipeTouchListener implements OnTouchListener { public static final int SWIPE_THRESHOLD = 50; public static final int SWIPE_VELOCITY_THRESHOLD = 50; protected GestureDetector gestureDetector; final int IDLE = 0; final int TOUCH = 1; final int PINCH = 2; int touchState; double dist0, distCurrent; private Context CONTEXT; public OnSwipeTouchListener(Context ctx) { gestureDetector = new GestureDetector(ctx, new GestureListener()); CONTEXT = ctx; } public OnSwipeTouchListener() { } public void onSwipeRight() { } public void onSwipeLeft() { } public void onSwipeTop(float diffY) { } public void onSwipeBottom(float diffY) { } public void onDoubleTap(){ } // public abstract void onSwipeRight(); // // public abstract void onSwipeLeft() ; // // public abstract void onSwipeTop(double diff) ; // // public abstract void onSwipeBottom(double diff); // // public abstract void onPinch(double distCurrent) ; @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } private final class GestureListener extends SimpleOnGestureListener { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { boolean result = false; try { float diffY = e2.getY() - e1.getY(); float diffX = e2.getX() - e1.getX(); if(Math.abs(diffX) > Math.abs(diffY)) { if(Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { if(diffX > 0) { onSwipeRight(); } else { onSwipeLeft(); } } result = true; } else if(Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { if(diffY > 0) { onSwipeBottom(diffY); } else { onSwipeTop(diffY); } result = true; } } catch(Exception exception) { exception.printStackTrace(); } return result; } @Override public boolean onDoubleTap(MotionEvent e) { OnSwipeTouchListener.this.onDoubleTap(); return super.onDoubleTap(e); } } } onFling() }

swipeLeft()

现在是问题所在。 我正在使用库swipeRight(),并使用上述swipeTop()方法将@Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { boolean result = false; try { if(Math.abs(velocityX) > Math.abs(velocityY)){ //Horizontal Swipe if(velocityX < 0){ //Swipe Left onSwipeLeft(); result = true; } else if(velocityX > 0){ //Swipe Right onSwipeRight(); result = true; } } else if(Math.abs(velocityY) > Math.abs(velocityX)){ //Vertical Swipe if(velocityY < 0){ //Swipe Top onSwipeTop(Math.abs(e1.getY() - e2.getY())); result = true; } else if(velocityY > 0){ //Swipe Bottom onSwipeBottom(Math.abs(e1.getY() - e2.getY())); result = true; } } } catch(Exception exception) { exception.printStackTrace(); } return result; } 设置为https://github.com/lsjwzh/RecyclerViewPager。 即使我已经设置了自定义onTouchListener,但似乎GestureDetector在自定义onFling()方法之后似乎正在获得滑动命令/手势。

我尝试设置onTouchListener,但是没有用。 设置自定义onTouchListener后,如何禁用将手势传递给recyclerViewPager?

0 个答案:

没有答案