How can I get my ListView to scroll?

ThaMe90 picture ThaMe90 · Mar 15, 2011 · Viewed 33.6k times · Source

I have a quite complex View build-up, and as a part of that, I have a ListView inside a LinearLayout within a ScrollView (and quite a lot more components, but they do not matter in this issue).

Now the whole activity scrolls nicely as it should, but the ListView has a limited height, and when the Items inside it surpass the height, the disappear of my screen. I've tried to place the ListView inside it's own ScrollView, but this doesn't work. When I try to scroll on the ListView, the main ScrollView is selected and my screen scrolls instead of the ListView.

My question may sound easy, but I haven't been able to fix this... Is it possible to make the ListView scrollable aswell?

The relevant XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <LinearLayout android:id="@+id/GlobalLayout" 
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical" >

        <ListView android:id="@+id/EndpointList"
                  android:choiceMode="multipleChoice"
                  android:layout_height="175dip"
                  android:layout_width="fill_parent" />

    </LinearLayout>

</ScrollView>

Answer

pawelzieba picture pawelzieba · Mar 15, 2011

Instead of ListView with other layouts inside ScrollView create one ListView with header and footer.

Add views that should be above ListView as a header:

addHeaderView(View v)

and that below as a footer:

addFooterView(View v)

Put everything what should be above ListView to header of ListView and the same with footer.

    LayoutInflater inflater = LayoutInflater.from(this);
    mTop    = inflater.inflate(R.layout.view_top, null);
    mBottom = inflater.inflate(R.layout.view_bottom, null);

    list.addHeaderView(mTop);
    list.addFooterView(mBottom);
    // add header and footer before setting adapter
    list.setAdapter(mAdapter);

In result you'll get one scrollable view.