Android list view inside a scroll view Android list view inside a scroll view android android

Android list view inside a scroll view


The shortest & easiest solution for any ChildView to scroll inside a ScrollView. Anything like ListView, RecyclerView, etc. You do not have to do anything special in code.

Just replace ScrollView with androidx.core.widget.NestedScrollView in your current xml and then magic happens.

Below is a sample xml code :

<?xml version="1.0" encoding="utf-8"?><androidx.core.widget.NestedScrollView    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent">    <androidx.appcompat.widget.LinearLayoutCompat        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        android:padding="16dp"        android:paddingBottom="20dp">        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="Recycler View inside a Scroll View"            android:textColor="@color/black"            android:textSize="@dimen/_20sp"            android:textStyle="bold" />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="8dp"            android:text="Below is a Recycler View as an example."            android:textSize="16sp" />        <androidx.recyclerview.widget.RecyclerView            android:id="@+id/recycler_view"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="8dp"            app:layout_constraintTop_toBottomOf="@id/et_damaged_qty" />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="8dp"            android:text="This textview automatically goes below the Recycler View."            android:textSize="16sp" />    </androidx.appcompat.widget.LinearLayoutCompat></androidx.core.widget.NestedScrollView>

Now you can get rid of all the ugly hacks we did to get around this nested scrolling.


The answer is simple and I am surprised it has yet to be answered here.

Use a Header View or/and Footer View on the list itself.Don't mix a ScrollView with a ListView or anything that can scroll. It's meant to be used with headers and footers :)

Essentially, take all the content above your ListView, put it in another .xml file as a layout and then in code inflate it and add it to the list as a header view.

i.e.

View header = getLayoutInflater().inflate(R.layout.header, null);View footer = getLayoutInflater().inflate(R.layout.footer, null);listView.addHeaderView(header);listView.addFooterView(footer);


I know it's been so long but I got this problem too, tried this solution and it's working. So I guess it may help the others too.

I add android:fillViewport="true" on the layout xml for the scrollView.So overall my ScrollView will be like this.

<ScrollView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/scrollView6"     android:fillViewport="true">

And it works like magic to me. the ListView that located inside my ScrollView expand to its size again.

Here is the full example code for the ScrollView and the ListView.

<ScrollView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/scrollView6" android:fillViewport="true">    <LinearLayout        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="match_parent">        ....        <ListView            android:layout_width="match_parent"            android:layout_height="match_parent"            android:id="@+id/lv_transList" android:layout_gravity="top"            android:layout_marginTop="5dp"/>        ....    </LinearLayout></ScrollView>