【絕對乾貨】完美解決AppBarLayout的滑動問題
前言
首先,我相信Android應用層開發的小夥伴們大部分都使用過AppBarLayout這個佈局吧。對,就是大家所熟悉的經典的CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout + TabLayout + NestedScrollView(不知道如何使用的可以參考這篇文章ofollow,noindex">https://www.jianshu.com/p/06c0ae8d9a96 )。也許大家都會認同以上佈局組合在實際應用場景中非常好用,然而,AppBaryLayout其實還是存在著一些滑動問題的。
問題描述
- 快速滑動AppBarLayout會出現回彈;
- 快速滑動AppBarLayout到摺疊狀態下,立馬下拉會出現抖動現象;
- 滑動AppBarLayout過程中,無法像RecyclerView那樣通過手指按下停止滾動。
問題分析
一切都是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.1' }
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的原始碼 。
題外話
如果覺得本文的內容有幫助到大家就給個讚唄~