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

如何在ConstraintLayout内的NestedScrollView中添加RecyclerView并解决不显示问题

问题核心原因

  1. NestedScrollView组件仅允许存在1个直接子元素,你的代码中直接在NestedScrollView下放置了LinearLayout和RecyclerView两个并列子元素,导致RecyclerView无法被正常测量渲染。
  2. RecyclerView与NestedScrollView默认存在滚动冲突,未关闭嵌套滚动时也会导致显示、滚动异常。

修复步骤

  • 将NestedScrollView内的所有子元素统一包裹在一个垂直方向的LinearLayout中,作为NestedScrollView的唯一直接子视图
  • 给RecyclerView添加android:nestedScrollingEnabled="false"属性,关闭嵌套滚动避免冲突
  • 移除NestedScrollView内部子元素上无效的ConstraintLayout约束属性(内部元素不属于ConstraintLayout直接子级,约束属性不生效)

修复后的完整XML代码

<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.MainFragment">
        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_default_back"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <androidx.core.widget.NestedScrollView
            android:id="@+id/scrollview"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:fillViewport="true"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/toolbar">

            <!-- 新增外层垂直LinearLayout作为NestedScrollView唯一直接子元素 -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/linear_layout"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_marginStart="20dp"
                    android:layout_marginTop="10dp"
                    android:orientation="horizontal">
                    <ImageView
                        android:id="@+id/imageView13"
                        android:layout_width="wrap_content"
                        android:layout_height="45dp"
                        app:srcCompat="@drawable/ic_baseline_person_add" />
                    <TextView
                        android:id="@+id/textView42"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:layout_marginTop="8dp"
                        android:layout_weight="1"
                        android:text="Received"
                        android:textSize="18sp" />
                </LinearLayout>

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="450dp"
                    android:nestedScrollingEnabled="false"/>
            </LinearLayout>
        </androidx.core.widget.NestedScrollView>
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

补充说明

如果需要RecyclerView高度自适应内容,可将它的android:layout_height属性修改为wrap_content,NestedScrollView会自动拉伸容纳所有内容。

内容的提问来源于stack exchange,提问作者Lavish Garg

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 06:18:03