NestedScrollView嵌套Recyclerview列表置顶问题

简要

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>
    

问题解决

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容