关于含RecyclerView的fragment来回切换时页面自动滑动到底部的解决方法

原因:

在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>

猜你喜欢

转载自www.cnblogs.com/yang12318/p/9091531.html
今日推荐