How to make a Fragment scrollable with a ListView
Asked Answered
E

1

5

I want a Fragment that has 2 ListView. I dont want the listView to scroll instead i just want the whole Fragment to scroll. Heres the code:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff" >

    <TextView
        android:id="@+id/day"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:gravity="center_horizontal"
        android:paddingTop="15dp"
        android:text="Monday"
        android:textColor="#000"
        android:textSize="55sp" />

    <TextView
        android:id="@+id/date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/day"
        android:gravity="center_horizontal"
        android:text="27 April, 2014"
        android:textColor="#000"
        android:textSize="45sp" />

    <ListView
        android:id="@+id/home_list_time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/date"
        android:layout_margin="10dp"
        android:background="@color/list_home_time_bg"
        android:divider="@android:color/white"
        android:dividerHeight="20.0sp" />

    <ListView
        android:id="@+id/home_list_stime"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/home_list_time"
        android:layout_margin="10dp"
        android:background="@color/list_home_stime_bg"
        android:divider="@android:color/white"
        android:dividerHeight="20.0sp" />

    </RelativeLayout>

When i run this code the 2nd listView gets its own scroll bar, is there a way to stop this and make the entire Fragment to scroll?

Emersonemery answered 8/7, 2014 at 12:12 Comment(1)
I think its a bad idea but have you tried to wrap the RelativeLayout in a ScrollView? I think you'll see some weird scrolling behaviour and/or the listviews height might not be what you expect. This is because the scrollview is somewhat aggressive when measuring an unpopulated listview.Alpenhorn
S
10

To stop the listview scrolling use -

listView.setScrollContainer(false);

To scroll fragment put it inside ScrollView -

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <!-- everything you already have -->
</ScrollView>

For more check - how-to-get-a-non-scrollable-listview.

NOTE:

If you put ListView inside a ScrollView then all the ListView does not stretch to its full height. Below is a method to fix this issue.

/**** Method for Setting the Height of the ListView dynamically.
 **** Hack to fix the issue of not showing all the items of the ListView
 **** when placed inside a ScrollView  ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

To use this method just pass the ListView inside this method :

ListView list1 = (ListView) view.findViewById(R.id.home_list_time);
setListViewHeightBasedOnChildren(list1);

ListView list2 = (ListView) view.findViewById(R.id.home_list_stime);
setListViewHeightBasedOnChildren(list2);

More here - android-list-view-inside-a-scroll-view.

Storeroom answered 8/7, 2014 at 12:17 Comment(1)
For some reason, now the listView just shows the first element and we need to scroll to see the rest and the entire fragment activity doesnt fill the mobile screen height. Your answer should have worked but i dont know whats wrong... Why is the listView still scrolling if we set it to false?Emersonemery

© 2022 - 2024 — McMap. All rights reserved.