android AppBarLayout 結(jié)合ViewPager或者 RecyclerView 快速滑動(dòng)底部加載更多的問題

特別說明 只是為了記錄自己遇到的問題

前段時(shí)間做項(xiàng)目 用到AppBarLayout 咔嚓咔嚓一頓寫,寫完了 測試說你這個(gè)加載更多數(shù)據(jù)時(shí) 數(shù)據(jù)沒有連在一塊(一頁15條加載更多時(shí)顯示18或者20條以后的數(shù)據(jù))后來網(wǎng)上查詢 滑動(dòng)沖突 可是查詢的都是數(shù)據(jù)抖動(dòng) 可能是自己查詢的姿勢不對,過段時(shí)間領(lǐng)導(dǎo)說必須改可是自己在忙新的東西,于是同時(shí)一頓搜索 實(shí)驗(yàn) 最后成了。
自己貼下代碼 讓自己記住

public class FixAppBarLayoutBehavior extends AppBarLayout.Behavior {
private OverScroller mScroller;

public FixAppBarLayoutBehavior() {
    super();
}

public FixAppBarLayoutBehavior(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        Object scroller = getSuperSuperField(this, "mScroller");
        if (scroller != null && scroller instanceof OverScroller) {
            OverScroller overScroller = (OverScroller) scroller;
            overScroller.abortAnimation();
        }
    }

    return super.onInterceptTouchEvent(parent, child, ev);
}

private Object getSuperSuperField(Object paramClass, String paramString) {
    Field field = null;
    Object object = null;
    try {
        field = paramClass.getClass().getSuperclass().getSuperclass().getDeclaredField(paramString);
        field.setAccessible(true);
        object = field.get(paramClass);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return object;
}

//fling上滑appbar然后迅速fling下滑recycler時(shí), HeaderBehavior的mScroller并未停止, 會導(dǎo)致上下來回晃動(dòng)
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target,
                           int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed,
            dxUnconsumed, dyUnconsumed, type);
    stopNestedScrollIfNeeded(dyUnconsumed, child, target, type);
}

@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child,
                              View target, int dx, int dy, int[] consumed, int type) {
    super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
    stopNestedScrollIfNeeded(dy, child, target, type);
}

private void stopNestedScrollIfNeeded(int dy, AppBarLayout child, View target, int type) {
    if (type == ViewCompat.TYPE_NON_TOUCH) {
        final int currOffset = getTopAndBottomOffset();
        if ((dy < 0 && currOffset == 0)
                || (dy > 0 && currOffset == -child.getTotalScrollRange())) {
            ViewCompat.stopNestedScroll(target, ViewCompat.TYPE_NON_TOUCH);
        }
    }
}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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