采用结构
PtrFrameLayout 嵌套一个带下拉刷新的RecyclerView
PtrFrameLayout是一个自定义下拉刷新布局RV内部Item包含一个横向滑动的RecyclerView在顶部
导致的问题:横向滑动RecyclerView时经常容易引起下拉刷新,这种体验很差
解决思路
继承RecyclerView,重写dispatchTouchEvent,根据ACTION_MOVE的方向判断是否调用getParent().requestDisallowInterceptTouchEvent去阻止父view拦截点击事件
通过继承PtrFrameLayout,重写requestDisallowInterceptTouchEvent方法,获取disallowIntercept来判断是否分发事件给父View(避免父View获取事件引起下拉操作)
@Override
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
disallowInterceptTouchEvent = disallowIntercept;
//子View告诉父容器不要拦截我们的事件的
super.requestDisallowInterceptTouchEvent(disallowIntercept);
}```
#### 具体源码
* 重写横向滑动RecyclerView
public class BetterRecyclerView extends RecyclerView {
private ViewGroup parent;
public BetterRecyclerView(Context context) {
super(context);
}
public BetterRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setNestParent(ViewGroup parent) {
this.parent = parent;
}
private int lastX = -1;
private int lastY = -1;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int x = (int) ev.getRawX();
int y = (int) ev.getRawY();
int dealtX = 0;
int dealtY = 0;
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
dealtX = 0;
dealtY = 0;
// 保证子View能够接收到Action_move事件
getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_MOVE:
dealtX += Math.abs(x - lastX);
dealtY += Math.abs(y - lastY);
// Log.i("dispatchTouchEvent", "dealtX:=" + dealtX);
// Log.i("dispatchTouchEvent", "dealtY:=" + dealtY);
// 这里是够拦截的判断依据是左右滑动,读者可根据自己的逻辑进行是否拦截
if (dealtX >= dealtY) {
getParent().requestDisallowInterceptTouchEvent(true);
} else {
getParent().requestDisallowInterceptTouchEvent(false);
}
lastX = x;
lastY = y;
break;
case MotionEvent.ACTION_CANCEL:
break;
case MotionEvent.ACTION_UP:
break;
}
return super.dispatchTouchEvent(ev);
}
* 重写PtrFrameLayout的部分源码
private boolean disallowInterceptTouchEvent = false;
@Override
public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
disallowInterceptTouchEvent = disallowIntercept;
super.requestDisallowInterceptTouchEvent(disallowIntercept);
}
@Override
public boolean dispatchTouchEvent(MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_UP:
this.requestDisallowInterceptTouchEvent(false);
disableWhenHorizontalMove(true);
break;
}
if (disallowInterceptTouchEvent) {
return dispatchTouchEventSupper(e);
}
return super.dispatchTouchEvent(e);
}