RecyclerView中嵌套RecyclerView或其他可滑動布局搶占焦點的問題的解決辦法
下面先看一下問題所導致的現象:
20170812171210726.gif
可以看到,當我們第一次打開app的時候,第一個item是沒有完整顯示的,給人的感覺是向上有了一段位置的偏移,這個問題就是RecyclerView中嵌套RecyclerView所導致的搶占焦點的問題。
具體的解決辦法就是給這個RecyclerView最外層的跟布局加上下面的兩個屬性:
android:focusableInTouchMode="true"
android:focusable="true"
即讓最外層的View獲取到焦點問題就解決了
下面貼一寫這個包含RecyclerView的布局對應的完整代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black_alpha_16"
android:orientation="vertical"
android:focusableInTouchMode="true"
android:focusable="true"
tools:context="com.merpyzf.kangyuanmilk.ui.home.HomeFragment">
<com.yalantis.phoenix.PullToRefreshView
android:id="@+id/pull_to_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</com.yalantis.phoenix.PullToRefreshView>
</LinearLayout>
問題解決后的運行效果:
20170812172513375.gif
這也是個奇葩問題,多個 recyclerview 相互 不僅會有滑動沖突而且還有焦點搶占問題導致莫名其妙的向上偏移,因此除非逼不得已還是不建議使用recyclerview嵌套來實現需求