-1

I am trying to add the RecyclerViewinside the NestedScrollViewwhich is inside a constraint layout,but it not getting add up i.e. 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 NestedScrollViewinside 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>
    
Zain
  • 37,492
  • 7
  • 60
  • 84
Lavish Garg
  • 235
  • 2
  • 9

1 Answers1

2

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>
Zain
  • 37,492
  • 7
  • 60
  • 84
  • Thank you so much for answering my query,can you pls tell from where we should read more about layouts,I donn't about these property of nestedscroll view before. – Lavish Garg Sep 27 '21 at 18:12
  • @LavishGarg Glad that could help.. the authentic place is always [the documentation](https://developer.android.com/guide/topics/ui/declaring-layout), but you can look around for medium articles or any authentic android course .. Android now trend for [material design](https://developer.android.com/large-screens) and [jetpack compose](https://developer.android.com/jetpack/compose).. Please accept the answer if it solved your problem – Zain Sep 27 '21 at 18:49
  • can you please see this,might you can help me with this issue,I haven't found any such decent tutorial for accessing background location in android 11. Question Link :-https://stackoverflow.com/questions/69824318/how-to-fetch-the-background-location-in-fragment-in-android-11-in-kotlin-none-o – Lavish Garg Nov 03 '21 at 11:50
  • Hey @LavishGarg I wish I could, but excuses as I didn't dig much on location services, hopefully someone else could help you. – Zain Nov 03 '21 at 12:08