Android Support Library 23.1的變化
原文 http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/1025/3622.ht
這篇文章只說說那些我關注的變化,完整文檔請看官網: http://android-developers.blogspot.com/2015/10/android-support-library-231.html
優化升級
RecyclerView的改變
這個版本最大的變化是動畫系統。使用 ItemAnimator 的 canReuseUpdatedViewHolder() 方法,你可以重用已存在的 ViewHolder ,啟動item內容動畫支持。新的 ItemHolderInfo 與相關的api讓ItemAnimator可以更靈活的在layout生命周期的正確時間收集想要的任何數據,把這些信息傳給animate callback。
注意這個新的api并不是向后兼容的。如果你之前實現了一個ItemAnimator,你可以轉而繼承 SimpleItemAnimator ,,它提供了用新api封裝了的舊api。你會發現ItemAnimator的有些方法完全被移除了。比如,如果你調用recyclerView.getItemAnimator().setSupportsChangeAnimations(false),你會發現這句代碼不能再被編譯了。你可以把它替換成:
ItemAnimator animator = recyclerView.getItemAnimator(); if (animator instanceof SimpleItemAnimator) { ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false); }
NavigationView開始支持自定義菜單
NavigationView 提供了創建一個抽屜導航(navigation drawer)的簡便方法,比如可以使用xml文件來創建菜單項。現在有了更新的擴展,能夠使用app:actionLayout 或者 MenuItemCompat.setActionView() 來為item設置自定義view。
bug修正
下面這兩個bug在第一個版本就不該存在。
修正了TabLayout的bug
在23.1.0之前的版本中,TabLayout有一個致命的錯誤,雖然從外觀上看起來不明顯,但是對手勢的體驗影響較大。
當tab的選項卡每項的寬度不一致的時候,下劃線的動畫效果有時候會出現抖動行為。這種行為在由較寬的選項卡到較窄的選項卡過渡的時候比較容易發生。
下圖演示了com.android.support:design:22.2.0版本下TabLayout的表現:
可以看出的確有抖動的行為,而且這還不是最明顯的。當然在每個tab的寬度相差不大的情況下不是那么明顯。
而23.1.0版本已經完美的修正了這個問題,下圖是23.1.0版本TabLayout的表現:
gif的質量可能有點影響效果,但是實際的表現完全是流暢無比的。
AppBarLayout子view的滾動問題
當AppBarLayout子view設置了 app:layout_scrollFlags的時候,比如下面的代碼在Toolbar上設置了app:layout_scrollFlags="scroll|enterAlways",隨著相關視圖(RecyclerView,或者NestedScrollView)的滾動,
Toolbar可能會出現只隱藏部分的情況,其實這個影響不大Toolbar本身高度有限。
<android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:layout_scrollFlags="scroll|enterAlways" /> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabMode="scrollable" style="@style/MyCustomTabLayout" /> </android.support.design.widget.AppBarLayout>
但是如果是想實現有頭部圖片的視差效果,比如類似下面的代碼:
<android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="@dimen/detail_backdrop_height" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:fitsSystemWindows="true"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:expandedTitleMarginStart="48dp" app:expandedTitleMarginEnd="64dp"> <ImageView android:id="@+id/backdrop" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:fitsSystemWindows="true" app:layout_collapseMode="parallax" /> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:layout_collapseMode="pin" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout>
就會出現如圖中的問題
可以看到,當我向上滑動的時候,要上劃兩次才完全隱藏圖片。
這造成一種不流暢的感覺。
嚴格說來這不算是一種bug,而是支持庫的作者有意為之,因為CoordinatorLayout所做的事情全都是根據某個滾動視圖的滾動距離設置相關視圖的位移。
但是這不光體驗不好,還不符合材料設計的規范。
因此有很多人提出來,在23.1.0版本,為了解決這個問題,layout_scrollFlags新增了一個flag,叫做SCROLL_FLAG_SNAP。
用法如下:
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
其實就是加了個snap標志。
它可以確保滾動的時候AppBarLayout子view要么不顯示要么顯示完。
聽起來很美好,但是用了才知道并不完美。
我自身的體驗是,這更加增大了操作難度,很多次我上劃無法滾上去,也有很多次下劃無法滾下來。
其實我個人認為,正確的做法是,當用戶的滑動速度到了一定大小(這代表滑動力度),只要上劃就一定隱藏圖片,只要下滑就一定顯示完圖片。
這些問題在第一個版本就該解決,因為第三方已經早就有人實現了自己的方案了,而chrisbanes卻挖了無數個坑。這是帶領開發者一起跳么。
不過有一點值得肯定,那就是現在AppBarLayout允許用戶從AppBarLayout的那部分就開始滾動,而不是必須在滾動視圖里。可以添加 DragCallback 來控制這個行為。