You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RecyclerView嵌套NestedScrollView后仅显示单个元素及空白的问题

问题解决:RecyclerView在NestedScrollView中仅显示单个Item

问题原因

你把AppBarLayout嵌套进NestedScrollView的做法破坏了CoordinatorLayout与AppBarLayout的原生联动逻辑,同时RecyclerView设置wrap_content高度时,禁用嵌套滚动后无法正确计算自身总高度,导致仅能显示单个Item。

修复方案

1. 调整布局层级

将AppBarLayout从NestedScrollView中移出,作为CoordinatorLayout的直接子View,让它和滚动区域实现正确的滚动联动。

2. 修正高度设置

  • 将NestedScrollView的layout_height改为match_parent,确保它占满剩余屏幕空间
  • 将RecyclerView的layout_height改为match_parent,让它在滚动容器内充分展开

修改后的完整布局代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    tools:context=".MainActivity">

    <!-- AppBarLayout作为CoordinatorLayout直接子View -->
    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/feed_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:id="@+id/feed_linear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/billabong"
                        android:paddingLeft="10dp"
                        android:paddingTop="10dp"
                        android:text="@string/instagram"
                        android:textColor="@color/white"
                        android:textSize="33sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:gravity="end">

                    <ImageView
                        android:id="@+id/notification_custom"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:layout_marginRight="15dp"
                        android:src="@drawable/heart_red" />

                    <ImageView
                        android:id="@+id/message_icon"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:layout_marginRight="12dp"
                        android:src="@drawable/messenger" />

                </LinearLayout>
            </LinearLayout>
        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <!-- NestedScrollView作为滚动容器,关联AppBarLayout的滚动行为 -->
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <LinearLayout
                android:id="@+id/feed_story_linear"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="@color/white"
                android:orientation="horizontal" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/feed_recyler"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false" />

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

关键注意点

  • 保留代码中recyclerView.setNestedScrollingEnabled(false)的设置,让NestedScrollView统一处理滚动事件
  • TextView的textSize建议用sp单位而非dp,适配不同屏幕的字体缩放
  • 移除CoordinatorLayout的android:gravity="center"属性,避免影响子View的布局位置

内容的提问来源于stack exchange,提问作者Mohd Zaid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 05:54:50