ViewPager+Fragment切換時,RecyclerView向上自動滑動
阿新 • • 發佈:2019-02-09
ViewPager+Fragment在專案中經常會遇到,最近一個專案就是這種情況,ViewPager+Fragment,每一個Fragment又有RecyclerView滑動監聽,但是在ViewPager切換頁面時,Fragment的RecyclerView會自動向上滑動把上面的Banner遮蓋住,試過一些方法,像是RecyclerView監聽滑動狀態,指定滑動位置等等,都沒有效果,最後嘗試禁止RecyclerView獲取焦點,在父佈局中加入
android:descendantFocusability="blocksDescendants"
使得RecyclerView不能獲取到焦點,最後解決問題,附上原始碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_hot" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
這樣就可以解決在Fragment中有RecyclerView時,ViewPager切換就不會自動滑動的情況了。