RecyclerView onTouch & onClick 触摸事件与点击事件

遇到一种情况需要处理,RecyclerView所在Activity需要处理滑动事件用于返回(onBackPressed)。

手势检测:

  @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
LogUtil.d(this,"fling");
if (e1.getX() - e2.getX() > verticalMinDistance
&& Math.abs(velocityX) > minVelocity) {
LogUtil.d(this, "fling left");
return true;
} else if (e2.getX() - e1.getX() > verticalMinDistance
&& Math.abs(velocityX) > minVelocity) {
LogUtil.d(this, "fling right");
activity.onBackPressed();
return true;
}
return false;
} @Override
public boolean onDown(MotionEvent e) {
LogUtil.d(this, "Down");
return false;
} @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
LogUtil.d(this, "scroll");
return false;
}

此处需要注意,onDown不能直接处理成true(开始就坑在这里,完全按普通触摸事件处理成返回true,导致click事件永远不会得到事件触发)

虽然官方文档建议处理触摸事件时,要将其置成true,表示开始处理触摸。但因为我们还有itemView的click事件要处理,所以onDown不能直接返回true.

绑定触摸事件:

  view.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
LogUtil.d("Base RecyclerView Event", "Touch");
mDetector.onTouchEvent(e);
return false;
} @Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) { } @Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { }
});
上一篇:BZOJ 1609: [Usaco2008 Feb]Eating Together麻烦的聚餐( LIS )


下一篇:Ansible 操作windows