NestedScrollView嵌套R(shí)ecyclerview列表置頂問題

簡(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è)置如下屬性解決該問題

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

問題解決

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容