【问题标题】:Intercept recycler view gesture swipe right and left, android?拦截回收站视图手势左右滑动,android?
【发布时间】:2018-06-05 10:26:47
【问题描述】:

我有一个 recyclerView。当用户向右或向左滑动时执行一些操作。 请注意,我不是要求滑动回收器视图项目,而是向左或向右整个回收器视图手势。

为此,我扩展了 View.OnTouchListener 并尝试获得如下手势

public class OnSwipeTouchListener implements View.OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeTouchListener(Context ctx) {
    gestureDetector = new GestureDetector(ctx, new GestureListener());
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @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();
                } else {
                    onSwipeTop();
                }
                result = true;
            }*/
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}


/*public void onSwipeTop() {
}

public void onSwipeBottom() {
}
*/
}

然后将其用作

recyclerViewComponent.setOnTouchListener(new OnSwipeTouchListener(context){
        @Override
        public void onSwipeRight() {
            super.onSwipeRight();
        }

        @Override
        public void onSwipeLeft() {
            super.onSwipeLeft();
        }
    });

但我没有收到回调。这有什么问题?

【问题讨论】:

  • 到目前为止你有什么进展吗?
  • @alex nopes 我还在想办法。如果您找到了解决方案,请告诉我。
  • 你有没有尝试过我的继承RecyclerView的建议?
  • @alex 我试过了,但它不起作用。我在父 recyclerview 中有一个子 recyclerview,我想在子 recyclerview 上向左/向右滑动
  • 好的,你有没有创建自己的类来扩展RecyclerView?

标签: android android-recyclerview ontouchlistener


【解决方案1】:

根据官方Google doc,在处理ViewGroups 时必须使用onInterceptTouchEvent(RecyclerView 扩展ViewGroup)。 因此,您必须扩展 RecyclerView 类才能覆盖其 onTouchInterceptTouchEvent 方法:

public class MyRecyclerView extends RecyclerView {

   @Override
   public boolean onInterceptTouchEvent(MotionEvent ev) {
       switch (MotionEventCompat.getActionMasked(ev)) {
          case MotionEvent.ACTION_MOVE: {
           [...]
          }
       }        
   }

}

然后您还必须更改布局 XML 文件以使用您的自定义 RecyclerView MyRecyclerView。

或者,您可以调整涉及 3rd 方组件/库的解决方案,例如 this 或 this。

【讨论】:

    【解决方案2】:

    @alex 响应是正确的,但作为解决方法,您始终可以在 onFling() 中返回 false,以便将事件分派给孩子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      • 2011-11-06
      • 1970-01-01
      • 2015-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多