ScrollView with ListView not scrolling - android

My problem with the following layout is that the layout doesn't scroll. My requirement is that I have to display 2 lists, which can be large at times. So I put the ListViews inside a ScrollView so that I can scroll to the bottom and look at the ListViews. I don't want the ListView to scroll

... I just want my ScrollView to behave the way it should. Any suggestions?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
        android:id="@+id/scrollViewContactDetails"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" android:fillViewport="true">


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >


            <TextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="22dp" />

            <LinearLayout
                android:id="@+id/numbersLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:orientation="vertical"
                android:weightSum="1" >

                <LinearLayout
                    android:id="@+id/numbersLayoutList"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:weightSum="1" >

                    <ListView
                        android:id="@+id/numbersList"
                        android:layout_width="match_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:dividerHeight="2dp" >
                    </ListView>
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/emailLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:orientation="horizontal"
                android:weightSum="1" >

                <LinearLayout
                    android:layout_width="120dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right|center"
                    android:gravity="top|right"
                    android:orientation="vertical" >

                    <TextView
                        android:id="@+id/emailId"
                        android:layout_width="120dp"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:gravity="right|center"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="#8d8d8d"
                        android:textSize="18dp" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/emailLayoutList"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"
                    android:layout_marginLeft="20dp"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:weightSum="1" >

                    <ListView
                        android:id="@+id/emailList"
                        android:layout_width="match_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:dividerHeight="2dp" >
                    </ListView>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

      

+3


source to share


5 answers


I spent days trying to figure out how to achieve this and couldn't find a solution. was a common word everywhere I looked. I didn't want to use LinearLayout or ViewGroup because I already created the entire UI with ListView and it looked amazing to me and everyone else. It worked well except the page didn't scroll. You should not put a ListView inside a ScrollView

I recently came across a question here and thought to give this answer a try. It works flawlessly!

Here's the solution:



public class Utility {
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); 
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}

      

Just call Utility.setListViewHeightBasedOnChildren(yourListView)

after you've assigned an adapter to your list and you're done!

Thanks a lot DougW

for your reply. Here is the original link How can I fit a ListView into a ScrollView without collapsing it?

+13


source


You shouldn't put listview in scrollview, it is not a good idea. If you don't want to scroll through it, you probably shouldn't be using listviews. Have you tried using LinearLayouts?



+3


source


Instead of putting two lists, you can use one listview and you can add both data or inflate both xml files in the adapter of that list. This will display the data in a single list and scroll through the data with a single scroll bar.

+1


source


Before calling the method below

public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); 
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

      

You have to remove the property 'android: fillViewport = "true" from the scrollview. If not, scrolling doesn't work.

+1


source


Try this way:

You removed scrolling from your layout because it doesn't work correctly for list view.

And also you will place the height "android:layout_height="fill_parent"

for the list so that it takes up the entire height of the parent layout.

so please fix some height manually for both lists.

0


source







All Articles