场景描述:因为横向滑动的,只要不是横向直线滑动 稍微有一点偏移就会造成横向滑动体验很差,断断续续,所以,需要我们触摸recyclerView的时候,就对父控件的下拉刷新进行关闭操作,然后抬手就开启下拉刷新,这样就解决了冲突事件。
1.重写recyclerView处理事件分发,调用到父控件进行事件处理
public class CustomRecyclerView extends RecyclerView {
public CustomRecyclerView(@NonNull Context context) {
this(context, null);
}
public CustomRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* 如果需要某些特定的手势这里直接自定义手势即可是否进行分发即可
* @param ev
* @return
*/
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
getParent().requestDisallowInterceptTouchEvent(true);
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent e) {
return super.onTouchEvent(e);
}
}
2.重写父控件,处理滑动是否相应刷新事件
public class CustomSwipeRefreshLayout extends SwipeRefreshLayout {
public CustomSwipeRefreshLayout(@NonNull Context context) {
this(context, null);
}
public CustomSwipeRefreshLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
private boolean isTouch = false;
@Override
public void requestDisallowInterceptTouchEvent(boolean b) {
this.isTouch = b;
super.requestDisallowInterceptTouchEvent(b);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()){
case MotionEvent.ACTION_UP://手松开就设置可以刷新,只要触摸到recyclerView就关闭。
setEnabled(true);
break;
}
if (isTouch) {
setEnabled(false);
}
return super.dispatchTouchEvent(ev);
}
}