前言
首先瑞佩,我相信Android應用層開發(fā)的小伙伴們大部分都使用過AppBarLayout這個布局吧扼脐。對赶盔,就是大家所熟悉的經典的CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout + TabLayout + NestedScrollView(不知道如何使用的可以參考這篇文章 http://www.reibang.com/p/06c0ae8d9a96 )凡傅。也許大家都會認同以上布局組合在實際應用場景中非常好用速警,然而,AppBaryLayout其實還是存在著一些滑動問題的强霎。
問題描述
- 快速滑動AppBarLayout會出現回彈;
- 快速滑動AppBarLayout到折疊狀態(tài)下蓉冈,立馬下拉會出現抖動現象城舞;
- 滑動AppBarLayout過程中轩触,無法像RecyclerView那樣通過手指按下停止?jié)L動。
問題分析
一切都是AppBarLayout的慣性滑動(fling)事件惹的禍家夺。
解決方案
gradle
Step 1. Add the JitPack repository in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency in your app build.gradle:
dependencies {
implementation 'com.github.yuruiyin:AppbarLayoutBehavior:v1.0.2'
}
xml
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:fitsSystemWindows="false"
app:layout_behavior="com.yuruiyin.appbarlayoutbehavior.AppBarLayoutBehavior"
>
......
</android.support.design.widget.AppBarLayout>
來自( https://github.com/yuruiyin/AppbarLayoutBehavior ) 脱柱。
其實就是從AppBarLayout的慣性滑動開刀,在合適的時機停止慣性滑動即可拉馋。對如何解決感興趣的同學榨为,可以直接查看源碼研究,也希望有遇到什么問題的小伙伴可以提issue來互相探討煌茴。
總結
本文雖然沒有詳細介紹問題的解決思路随闺,但是我相信本文絕對干貨,因為你用了上面所述的AppBarLayoutBehavior蔓腐,就可以開開心心地使用幾近完美的AppBarLayout了矩乐。難道這還不夠嗎? 對回论,當然不夠散罕,因為俗話說得好:我們不僅要知其然,還得知其所以然傀蓉。所以呢欧漱,還是希望大家自行去研究一下我提交到github上面的AppbarLayoutBehavior的源碼。
題外話
如果覺得本文的內容有幫助到大家就給個贊唄~