如何在放置在约束布局内的 NestedScrollView 中添加 Recycler View,尝试添加回收器视图但未成功

How to add Recycler View inside the NestedScrollView which is placed inside a constraint layout, tried to add recycler view but not getting succesful

我正在尝试在约束布局内的 NestedScrollView 内添加 RecyclerView,但它没有加起来,即 RecyclerView 即使在添加后也没有显示up,请帮助我,我正在添加它的 XML 代码。

我在做什么

  1. 在约束布局内添加NestedScrollView,在NestedScrollView.

    内的线性布局下方添加RecyclerView
     <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
                     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"
                     app:layout_constraintEnd_toEndOf="parent"
                     app:layout_constraintStart_toStartOf="parent"
                     app:layout_constraintTop_toTopOf="parent">
    
                     <ImageView
                         android:id="@+id/imageView13"
                         android:layout_width="wrap_content"
                         android:layout_height="45dp"
                         app:srcCompat="@drawable/ic_baseline_person_add"
                         tools:layout_editor_absoluteX="13dp"
                         tools:layout_editor_absoluteY="14dp" />
    
                     <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">
    
    
                 </androidx.recyclerview.widget.RecyclerView>
    
             </androidx.core.widget.NestedScrollView>
    
    
         </androidx.constraintlayout.widget.ConstraintLayout>
     </layout>
    

NestedScrollView 中有两个直接视图的问题,`NestedScrollView 中应该有一个根布局,然后你可以在这个根中嵌套任何其他布局。

要解决您的问题,您必须将 LinearLayoutRecyclerView 包裹在任意选择的根中 LinearLayout:

<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
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            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"
                 app:layout_constraintEnd_toEndOf="parent"
                 app:layout_constraintStart_toStartOf="parent"
                 app:layout_constraintTop_toTopOf="parent">

                 <ImageView
                     android:id="@+id/imageView13"
                     android:layout_width="wrap_content"
                     android:layout_height="45dp"
                     app:srcCompat="@drawable/ic_baseline_person_add"
                     tools:layout_editor_absoluteX="13dp"
                     tools:layout_editor_absoluteY="14dp" />

                 <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"/>

        </LinearLayout>

     </androidx.core.widget.NestedScrollView>


 </androidx.constraintlayout.widget.ConstraintLayout>
</layout>