RecyclerView嵌套问题

本文介绍了解决RecyclerView嵌套RecyclerView时的焦点抢占问题及ScrollView嵌套RecyclerView时的滑动冲突和内容显示不全问题的方法。对于焦点抢占问题,通过设置item布局中的RecyclerView为可获取焦点状态;对于滑动冲突问题,推荐使用NestedScrollView替代ScrollView。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当我们布局遇到一些复杂的布局时,或是一些特殊要求的布局时,会用到RecyclerView嵌套RecyclerView或者ScrollView嵌套RecyclerView的情况,但是使用这两种嵌套方式进场会遇到滑动冲突,焦点抢占或是RecyclerView内容显示不全等问题,那么怎么解决呢?

1.RecyclerView嵌套RecyclerView

我们经常在项目的类似朋友圈场景中使用RecyclerView嵌套RecyclerView,但是焦点抢占问题是这种嵌套的一个大问题。

上图中第一张图是嵌套时焦点抢占原因默认打开页面的显示情况,第二张图是我们想要的默认打开页面的显示情况。

先看一下布局文件

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.joh.demo.scrowviewdemo.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycle_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="com.joh.demo.scrowviewdemo.MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/text_one" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/text_one" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" />

</LinearLayout>

出现上图中的情况是因为,item布局中的RecyclerView抢占了外层RecyclerView的焦点,解决方法也很简单,只需要把焦点还给外层RecyclerView就可以了,那么如何做呢?只需要在item_layout这个布局文件的最外层布局中添加以下代码即可

android:focusable="true"
android:focusableInTouchMode="true"

然后编辑运行,你会发现问题已经解决了

如果使用RecyclerView添加头布局时,记得在头布局文件的最外层布局添加上面两行代码,不然会出现item抢占头布局焦点的问题

2.ScrollView嵌套RecyclerView

ScrollView嵌套RecyclerView常见的问题是滑动冲突和RecyclerView内容显示不全的问题,笔者遇到这个问题时在网上找了好多,大多建议重写LayoutManager(相当于ScrollView嵌套ListView时,直接测量ListView的高度,禁止ListView的滑动事件一样),这种方法也的确可行,但是需要重写LayoutManager,太麻烦。那么有没有可替代的方案呢?NestedScrollView解决了ScrollView嵌套RecyclerView的滑动冲突问题,如果需要使用ScrollView嵌套RecyclerView的布局时,建议大家可以考虑试着用NestedScrollView替代一下ScrollView,这样就可以解决掉滑动冲突和RecyclerView内容显示不全的问题。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.joh.demo.scrowviewdemo.MainActivity">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycle_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:nestedScrollingEnabled="false" />
        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

</LinearLayout>

效果图

使用NestedScrollView嵌套RecyclerView,需要在RecyclerView的属性中添加android:nestedScrollingEnabled=“false”,不然还是会出现滑动冲突。

demo地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值