When a ListView is put inside a ScrollView with (or without) other views it takes as much space is available in the ScrollView. So let’s say there are three elements like this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="true" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="19dp" android:text="@string/hello_world" /> <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/ic_launcher" /> </LinearLayout> </ScrollView>
If you notice, ListView is in the middle (second element). So it’ll take up the entire height if there are enough elements pushing the ImageView off the bottom edge and hence never visible. Now if it’s the last element then also it’ll take the entire remaining height if enough elements are there and you’ll be able to scroll it perfectly but that’s a user experience that you might not want. You might want to scroll the ScrollView instead of the ListView. So what that means is you might want to be able to scroll even when you tap on the ImageView/TextView area and drag upwards/downwards and the entire content should scroll rather than the particular ListView area.
What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.
One of the solution to this problem is to set a fixed height on the ListView. But what do you do when there are variable items in it ? Well, then you just calculate the height dynamically and set it.
You’ll find the solution with all the code in the following answers:
Fairly simple I think. You can modify those codes to adapt them for GridView too, if required.
Hope that helps!