簡(jiǎn)要
NestedScrollView嵌套R(shí)ecyclerview,在NestedScrollView 中addView 添加不同的布局樣式。當(dāng)從列表頁(yè)面啟動(dòng)新的頁(yè)面Recyclerview列表置頂了。
示例代碼
<androidx.core.widget.NestedScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/xxxx_info"
layout="@layout/layout_item2" />
<com.xxxx.widget.ClassLayout
android:id="@+id/layoutBeforeClass"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
解決方式
Recyclerview頂上去,是因?yàn)镽ecyclerview搶占焦點(diǎn)從而出現(xiàn)該問題。對(duì)此設(shè)置如下屬性解決該問題
- 在NestedScrollView xml 布局節(jié)點(diǎn)設(shè)置 android:focusableInTouchMode="true"
<androidx.core.widget.NestedScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:focusableInTouchMode="true"
android:layout_height="match_parent">
// 省略部分代碼 、、、、、
</androidx.core.widget.NestedScrollView>
-
在Recyclerview 直屬父節(jié)點(diǎn)布局設(shè)置如下屬性
android:focusable="true"
android:descendantFocusability="blocksDescendants"<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" android:descendantFocusability="blocksDescendants" android:orientation="vertical"> <com.xxxx.widget.ClassLayout android:id="@+id/layoutBeforeClass" android:layout_width="match_parent" android:layout_height="wrap_content"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
問題解決