1. 程式人生 > >關於含RecyclerView的fragment來回切換時頁面自動滑動到底部的解決方法

關於含RecyclerView的fragment來回切換時頁面自動滑動到底部的解決方法

get 切換 屬性 odi encoding 1.0 linear UC div

原因:

在fragment中來回切換時RecyclerView獲得了焦點,而RecyclerView的 focusableOnTouchMode屬性默認是true,所以在切換時RecyclerView自動獲得焦點就滾動到了底部。

解決辦法:

將RecyclerView上面的控件的設置android:focusableInTouchMode="true"(若無上方控件建議加一個不影響布局的View),如:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 <View 7 android:layout_width="match_parent" 8 android:layout_height="2px" 9 android:background="#bfbfbf" 10 android:focusable="true"
11 android:focusableInTouchMode="true"/> 12 <android.support.v7.widget.RecyclerView 13 android:layout_width="match_parent" 14 android:layout_height="match_parent" 15 android:paddingRight="8dp" 16 android:paddingLeft="8dp" 17 android:fadingEdge
="none"> 18 </android.support.v7.widget.RecyclerView> 19 </LinearLayout>

關於含RecyclerView的fragment來回切換時頁面自動滑動到底部的解決方法