Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android list view inside a scroll view

I have an android layout which has a scrollView with a number of elements with in it. At the bottom of the scrollView I have a listView which is then populated by an adapter.

The problem that I am experiencing, is that android is excluding the listView from the scrollView as the scrollView already has a scroll-able function. I want the listView to be as long as the content is and for the master scroll view to be scroll-able.

How can I achieve this behavior?

Here is my main layout:

<ScrollView     android:id="@+id/scrollView1"     android:layout_width="match_parent"     android:layout_height="0dp"     android:layout_weight="2"     android:fillViewport="true"     android:gravity="top" >      <LinearLayout         android:id="@+id/foodItemActvity_linearLayout_fragments"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="vertical" >     </LinearLayout>  </ScrollView> 

I then programmatically add my components to the linearlayour with the id: foodItemActvity_linearLayout_fragments. Below is one of the views that is loaded into that linearlayout. This is the one giving me trouble with the scrolls.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="vertical" >      <TextView        android:id="@+id/fragment_dds_review_textView_label"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Reviews:"        android:textAppearance="?android:attr/textAppearanceMedium" />     <ListView        android:id="@+id/fragment_dds_review_listView"        android:layout_width="match_parent"        android:layout_height="wrap_content">    </ListView> </LinearLayout> 

My adapter then fills up this list view.

Here is an image from the android hierarchy viewer when I click on the master scrollView:

Android List View inside a scroll view

As you can see, it is excluding the reviews listView.

I should be able to scroll the page down and see 8 reviews, but instead it only shows me those 3, and I can scroll on the tiny part where the reviews are. I want a global page scroll

like image 852
Zapnologica Avatar asked Aug 21 '13 20:08

Zapnologica


People also ask

What is nested scroll view?

NestedScrollView is just like ScrollView , but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

What is scroll view and list view?

In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.

Is a view group that displays a list of scrollable items?

ListView : is a view group that displays a list of scrollable item.

Is a default scrollable which does not use other scroll view?

Android supports vertical scroll view as default scroll view. Vertical ScrollView scrolls elements vertically. Android uses HorizontalScrollView for horizontal ScrollView.


2 Answers

For any Child view to scroll inside a ScrollView. Anything like ListView, RecyclerView, etc. You just have to 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 you did to get around with nested scrolling.

like image 129
arshu Avatar answered Oct 12 '22 23:10

arshu


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); 
like image 38
ericosg Avatar answered Oct 13 '22 01:10

ericosg