帶下拉刷新的RecyclerView嵌套橫向RecyclerView事件沖突

實(shí)際效果圖

采用結(jié)構(gòu)

  • PtrFrameLayout 嵌套一個帶下拉刷新的RecyclerView
    PtrFrameLayout是一個自定義下拉刷新布局

  • RV內(nèi)部Item包含一個橫向滑動的RecyclerView在頂部

導(dǎo)致的問題:橫向滑動RecyclerView時經(jīng)常容易引起下拉刷新,這種體驗(yàn)很差


解決思路

  1. 繼承RecyclerView,重寫dispatchTouchEvent,根據(jù)ACTION_MOVE的方向判斷是否調(diào)用getParent().requestDisallowInterceptTouchEvent去阻止父view攔截點(diǎn)擊事件

  2. 通過繼承PtrFrameLayout,重寫requestDisallowInterceptTouchEvent方法,獲取disallowIntercept來判斷是否分發(fā)事件給父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);
// 這里是夠攔截的判斷依據(jù)是左右滑動,讀者可根據(jù)自己的邏輯進(jìn)行是否攔截
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);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容