最近有一个需求,RecyclerView外层嵌套了一个FrameLayout,想实现点击整个FrameLayout都能响应一个事件,结果给FrameLayout加上点击事件后,点击RecyclerView范围无效。可能是RecyclerView截获了click事件,阻止了点击的透传,做了以下处理解决
recyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
myLayout.performClick(); //模拟父控件的点击
}
return false;
}
});
一开始这样改的,可以滑动列表时,容易误触点击事件,后来改良下
recyclerView.setOnTouchListener(new OnTouchListener() {
float downX = 0;
float downY = 0;
float moveX = 0;
float moveY = 0;
long currentMS = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
int touchSlop = ViewConfiguration.get(DIDIApplication.getAppContext()).getScaledTouchSlop();
int tapTimeout = ViewConfiguration.getTapTimeout();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
moveX = 0;
moveY = 0;
// 获取系统时间
currentMS = System.currentTimeMillis();
break;
case MotionEvent.ACTION_MOVE:
moveX += Math.abs(event.getX() - downX);//X轴距离
moveY += Math.abs(event.getY() - downY);//y轴距离
downX = event.getX();
downY = event.getY();
break;
case MotionEvent.ACTION_UP:
long moveTime = System.currentTimeMillis() - currentMS;//移动时间
//判断点击还是滑动
if (moveTime < tapTimeout || (moveX < touchSlop && moveY < touchSlop)) {
CouponGroupView.this.performClick();
return true; //不再执行后面的事件
}
break;
}
return false;
}
});