美文网首页
NestedScrollView嵌套Recyclerview列表

NestedScrollView嵌套Recyclerview列表

作者: 技术客栈 | 来源:发表于2021-03-06 11:37 被阅读0次

简要

NestedScrollView嵌套Recyclerview,在NestedScrollView 中addView 添加不同的布局样式。当从列表页面启动新的页面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顶上去,是因为Recyclerview抢占焦点从而出现该问题。对此设置如下属性解决该问题

  1. 在NestedScrollView xml 布局节点设置 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 直属父节点布局设置如下属性
    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>
    

问题解决

相关文章

网友评论

      本文标题:NestedScrollView嵌套Recyclerview列表

      本文链接:https://www.haomeiwen.com/subject/gubjqltx.html