Android6.0以上關於RecyclerView顯是不全的問題
阿新 • • 發佈:2018-08-26
strong text scroll lin 文件 nco pad one rda
Android6.0以上關於RecyclerView顯是不全的問題
需求描述
適配的時候發現Android 6.0以上,RecyclerView顯示不全,以下是沒有問題的。
這個時候查看布局文件,可以看到RecyclerView是嵌套在ScrollView中的。
<ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" android:paddingLeft="16dp" android:paddingRight="16dp" /> </ScrollView>
解決方法
第一種解決方法
方法
在RecyclerView上在嵌套上一層RelativeLayout,添加屬性android:descendantFocusability=”blocksDescendants”(當一個view獲取焦點時,viewGroup會覆蓋子類控件而直接獲取焦點)解決嵌套卡頓的問題。
註意,設立只能使用RelativeLayout,使用FrameLayout、LinearLayout等其他常用的布局控件是不行的。親測。
代碼
<ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" android:paddingLeft="16dp" android:paddingRight="16dp" /> </RelativeLayout> </ScrollView>
第二種解決方法
方法
使用android.support.v4.widget.NestedScrollView替代ScrollView,並在代碼中設置recyclerView.setNestedScrollingEnabled(false)(設置是否允許嵌套滑動)。既可以填ITEM顯示不全的坑,又可以填嵌套滑動卡頓的坑。
代碼
<android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" android:paddingLeft="16dp" android:paddingRight="16dp" /> </android.support.v4.widget.NestedScrollView>
recyclerView.setNestedScrollingEnabled(false);
總結
相比較而言,個人覺得第二種解決方法更好,第一種方法布局嵌套,總有一種想讓人優化的沖動,所以覺得第二種解決方法更好。
Android6.0以上關於RecyclerView顯是不全的問題