AppBarLayout快速滑動(dòng)導(dǎo)致回彈的解決方案

/**
 * @author JayChan
 * @desc AppBarLayout快速滑動(dòng)導(dǎo)致回彈的解決方案,在AppBarLayout的布局里使用這個(gè)Behavior即可
 * @date 2018/12/25  10:30
 */
public class AppBarLayoutBehavior extends AppBarLayout.Behavior {
    private static final int TYPE_FLING = 1;

    private boolean isFlinging;
    private boolean shouldBlockNestedScroll;

    public AppBarLayoutBehavior() {
    }

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

    @Override
    public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
        KLog.i("AppBarLayoutBehavior", "onInterceptTouchEvent:" + child.getTotalScrollRange());
        shouldBlockNestedScroll = false;
        if (isFlinging) {
            shouldBlockNestedScroll = true;
        }
        return super.onInterceptTouchEvent(parent, child, ev);
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed, int type) {
        //注意看ViewCompat.TYPE_TOUCH
        KLog.i("AppBarLayoutBehavior", "onNestedPreScroll:" + child.getTotalScrollRange() + " ,dx:" + dx + " ,dy:" + dy + " ,type:" + type);

        //返回1時(shí),表示當(dāng)前target處于非touch的滑動(dòng),
        //該bug的引起是因?yàn)閍ppbar在滑動(dòng)時(shí),CoordinatorLayout內(nèi)的實(shí)現(xiàn)NestedScrollingChild2接口的滑動(dòng)子類還未結(jié)束其自身的fling
        //所以這里監(jiān)聽子類的非touch時(shí)的滑動(dòng),然后block掉滑動(dòng)事件傳遞給AppBarLayout
        if (type == TYPE_FLING) {
            isFlinging = true;
        }
        if (!shouldBlockNestedScroll) {
            super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
        }
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dxConsumed, int dyConsumed, int
            dxUnconsumed, int dyUnconsumed, int type) {
        KLog.i("AppBarLayoutBehavior", "onNestedScroll: target:" + target.getClass() + " ," + child.getTotalScrollRange() + " ,dxConsumed:"
                + dxConsumed + " ,dyConsumed:" + dyConsumed + " " + ",type:" + type);
        if (!shouldBlockNestedScroll) {
            super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
        }
    }

    @Override
    public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout abl, View target, int type) {
        super.onStopNestedScroll(coordinatorLayout, abl, target, type);
        isFlinging = false;
        shouldBlockNestedScroll = false;
    }
}

在布局文件中引用


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

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

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,154評(píng)論 1 92
  • 本文參考自: Google的官方權(quán)威適配文檔 郭霖:Android官方提供的支持不同屏幕大小的全部方法 Storm...
    M悇芐冋憶閱讀 13,269評(píng)論 5 56
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,659評(píng)論 1 32
  • 關(guān)于資源 適用于計(jì)算機(jī)程序的資源是與程序可執(zhí)行代碼相關(guān)的數(shù)據(jù)文件。資源可以通過將代碼之外的復(fù)雜數(shù)據(jù)集或圖形內(nèi)容創(chuàng)建...
    nicedayCoco閱讀 736評(píng)論 0 0
  • 有時(shí)候,感覺自己挺糾結(jié)和矯情的。 怕太閑,怕太忙。 上班的時(shí)候想休假,休假久了想上班。 你說,我咋這么賤呢? 前段...
    凌少GG閱讀 337評(píng)論 0 0

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