關於含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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM