Search code examples
androidandroid-layoutlistviewandroid-coordinatorlayout

Android scroll whole screen instead of only listview


I have the following layout structure:

RelativeLayout
  -LinearLayout(Vertical)
  |  ...
  |  ...
  -LinearLayout(Vertical)
     ListView

The ListView has a bunch of items set by an ArrayAdapter. Currently, only the ListView is scrollable, the first LinearLayout stays in its place. However, I want the whole screen to be scrollable, so if the user scrolls far enough, the ListView will potentially occupy the whole screen, if there are enough list items.

This image illustrates the desired behaviour (before and after scrolling)Desired layout behaviour before and after scrolling The red box is the first LinearLayout, and the green box is the LinearLayout with the ListView

Thank you for your precious help!


Solution

  • If you don't have to use the RelativeLayout you can try a different structure using the CoordinatorLayout like this:

    <CoordinatorLayout >
        <AppBarLayout >
            <CollapsingToolbarLayout >
                 <!-- first layout with the property layout_scrollFlags="scroll" -->
            </CollapsingToolbarLayout >
        </AppBarLayout >
    
        <ListView > <!-- or RecyclerView to be more updated -->
    </CoordinatorLayout >
    

    If you have to use the RelativeLayout you can choose to implement all in a Scrollview (bad performarce and reinvent the wheel) or you can add your view as a header of the listview

    -----EDIT just for clarity here is my layout

    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="it.italia.playground.CollapseActivity">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fitsSystemWindows="true"
                    android:padding="@dimen/padding16"
                    app:layout_collapseMode="parallax">
    
                    <ImageView
                        android:id="@+id/image"
                        android:layout_width="150dp"
                        android:layout_height="150dp"
                        android:layout_alignParentTop="true"
                        android:layout_centerInParent="true"
                        android:maxHeight="@dimen/padding16"
                        android:scaleType="centerCrop"
                        android:src="@drawable/img"/>
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/image"
                        android:layout_centerInParent="true"
                        android:layout_marginTop="@dimen/padding16"
                        android:text="text title"
                        />
                </RelativeLayout>
    
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    
            </android.support.design.widget.CollapsingToolbarLayout>
    
        </android.support.design.widget.AppBarLayout>
    
        <include layout="@layout/content_layout"></include>
    
    
    </android.support.design.widget.CoordinatorLayout>