简体   繁体   中英

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

I am trying to add the RecyclerView inside the NestedScrollView which is inside a constraint layout,but it not getting add up ie RecyclerView is not showing up even after adding it up,please help me,I am adding the XML code of it.

what I am doing

  1. Adding the NestedScrollView inside the constraint layout and adding the RecyclerView below the linear layout which is within the NestedScrollView .

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

The problem that you have two direct views inside the NestedScrollView , and There should be a single root layout within the `NestedScrollView, and then you can nest any other layout within this root.

To fix your problem, you have to wrap the LinearLayout & the RecyclerView within a root which is arbitrary picked as 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM