Multiple ListViews inside a ScrollView
Asked Answered
B

6

9

I have a complex xml file and I really need a ScrollView. Please Don't mind the Ids as I have changed them. The ScrollView here doesn't work.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true">


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


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

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/black">


    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:layout_weight="1">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/BT1" 
                android:textColor="@color/white"
                android:textStyle="bold"         
                android:gravity="center_vertical|left" 
                android:paddingLeft="2dp" 
                android:textSize="13sp"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/BTS1" 
                android:textColor="@color/white"    
                android:paddingLeft="4dp"
                android:textSize="13sp"/>
    </LinearLayout>

    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"  
            android:gravity="center_vertical|right"
            android:layout_weight="1">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/textView7"
                android:textSize="13sp"
                android:textColor="@color/white"
                android:textStyle="bold" />
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/BT1CRR" android:textColor="@color/white" 
                android:paddingRight="8dp" android:textSize="13sp"
                android:paddingLeft="2dp"/>
    </LinearLayout>


</LinearLayout>

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">


    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/textView" android:textColor="@color/white"
            android:layout_weight="3" android:gravity="center"
            android:background="@drawable/table_header_cell_first" 
            android:textStyle="bold"
            android:textSize="13sp"/>
    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="R"
            android:id="@+id/textView1" android:textColor="@color/white" 
            android:gravity="center" android:background="@drawable/table_header_cell"
            android:textSize="13sp" android:textStyle="bold" android:layout_weight="1"
            />
    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="B"
            android:id="@+id/textView2" android:textColor="@color/white" 
            android:gravity="center" android:background="@drawable/table_header_cell"
            android:textStyle="bold" android:textSize="13sp"
            android:layout_weight="1"/>
    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="4s"
            android:id="@+id/textView3" android:textColor="@color/white" 
            android:gravity="center" android:background="@drawable/table_header_cell"
            android:textSize="13sp" android:textStyle="bold"
            android:layout_weight="1"/>
    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="6s"
            android:id="@+id/textView4" android:textColor="@color/white" 
            android:layout_weight="1"
            android:gravity="center" android:background="@drawable/table_header_cell"
            android:textSize="13sp" android:textStyle="bold"/>
    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="SR"
            android:id="@+id/textView5" android:textColor="@color/white" 
            android:layout_weight="1"
            android:gravity="center" android:background="@drawable/table_header_cell"
            android:textSize="13sp" android:textStyle="bold"/>

</LinearLayout>


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




<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:baselineAligned="false">


    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:layout_weight="1"
            android:baselineAligned="false">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/BT2" android:textStyle="bold" 
                android:textColor="@color/white"
                android:gravity="center_vertical|left" android:paddingLeft="2dp" 
                android:textSize="13sp"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/BTS2" android:textSize="13sp" 
                android:textColor="@color/white"
                android:paddingLeft="4dp"/>
    </LinearLayout>


</LinearLayout>

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/textViewBH" android:textColor="@color/white"
            android:layout_weight="3" android:gravity="center"
            android:background="@drawable/table_header_cell_first" 
            android:textSize="13sp"
            android:textStyle="bold"/>

    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="O"
            android:id="@+id/textViewBOH" android:textColor="@color/white" 
            android:layout_weight="1"
            android:gravity="center" android:background="@drawable/table_header_cell"
            android:textSize="13sp" android:textStyle="bold" android:clickable="false"
            />
    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="M"
            android:id="@+id/textViewBMH" android:textColor="@color/white" 
            android:layout_weight="1"
            android:gravity="center" android:background="@drawable/table_header_cell"
            android:textStyle="bold" android:textSize="13sp"/>
    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="R"
            android:id="@+id/textViewBRH" android:textColor="@color/white" 
            android:layout_weight="1"
            android:gravity="center" android:background="@drawable/table_header_cell"
            android:textSize="13sp" android:textStyle="bold"/>
    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="W"
            android:id="@+id/textViewBWH" android:textColor="@color/white" 
            android:layout_weight="1"
            android:gravity="center" android:background="@drawable/table_header_cell"
            android:textSize="13sp" android:textStyle="bold"/>
    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/textViewBECOH" android:textColor="@color/white" 
            android:layout_weight="1"
            android:gravity="center" android:background="@drawable/table_header_cell"
            android:textSize="13sp" android:textStyle="bold"/>

</LinearLayout>


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

    <ListView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/listView2"/>
</LinearLayout>

</LinearLayout>


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

    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/black">


        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="1">

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:id="@+id/BT2" android:textStyle="bold" 
                    android:textColor="@color/white"
                    android:gravity="center_vertical|left" android:paddingLeft="2dp" 
                    android:textSize="13sp"/>

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:id="@+id/BTS2" android:textSize="13sp" 
                    android:textColor="@color/white"
                    android:paddingLeft="4dp"/>
        </LinearLayout>

        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:layout_weight="1"  
                android:gravity="center_vertical|right">

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:id="@+id/textView7" android:textStyle="bold" 
                    android:textColor="@color/white"
                    android:textSize="13sp"/>
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0.00"
                    android:id="@+id/Bae2C" android:textColor="@color/white" 
                    android:paddingLeft="2dp"
                    android:paddingRight="8dp" android:textSize="13sp"/>
        </LinearLayout>


    </LinearLayout>
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">


        <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/textView" android:textColor="@color/white"
                android:layout_weight="3" android:gravity="center"
                android:background="@drawable/table_header_cell_first" 
                android:textSize="13sp"
                android:textStyle="bold"/>
        <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="R"
                android:id="@+id/textView9" android:textColor="@color/white" 
                android:layout_weight="1"
                android:gravity="center" 
                android:background="@drawable/table_header_cell"
                android:textSize="13sp" android:textStyle="bold" 
                android:clickable="false"
                />
        <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/textView10" android:textColor="@color/white" 
                android:layout_weight="1"
                android:gravity="center" 
                android:background="@drawable/table_header_cell"
                android:textStyle="bold" android:textSize="13sp"/>
        <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/textView11" android:textColor="@color/white" 
                android:layout_weight="1"
                android:gravity="center" 
                android:background="@drawable/table_header_cell"
                android:textSize="13sp" android:textStyle="bold"/>
        <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/textView4" android:textColor="@color/white" 
                android:layout_weight="1"
                android:gravity="center" 
                android:background="@drawable/table_header_cell"
                android:textSize="13sp" android:textStyle="bold"/>
        <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/textView5" android:textColor="@color/white" 
                android:layout_weight="1"
                android:gravity="center" 
                android:background="@drawable/table_header_cell"
                android:textSize="13sp" android:textStyle="bold"/>

    </LinearLayout>


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




    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/black"
            android:baselineAligned="false">


        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="1"
                android:baselineAligned="false">
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:id="@+id/Bw1" android:textStyle="bold" 
                    android:textColor="@color/white"
                    android:gravity="center_vertical|left" android:paddingLeft="2dp" 
                    android:textSize="13sp"/>
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:id="@+id/BoSe1" android:textSize="13sp" 
                    android:textColor="@color/white"
                    android:paddingLeft="4dp"/>
        </LinearLayout>


    </LinearLayout>
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">


        <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/textViewBH" android:textColor="@color/white"
                android:layout_weight="3" android:gravity="center"
                android:background="@drawable/table_header_cell_first" 
                android:textSize="13sp"
                android:textStyle="bold"/>
        <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/textViewBOH" android:textColor="@color/white" 
                android:layout_weight="1"
                android:gravity="center" 
                android:background="@drawable/table_header_cell"
                android:textSize="13sp" android:textStyle="bold" 
                android:clickable="false"
                />
        <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/textViewBMH" android:textColor="@color/white" 
                android:layout_weight="1"
                android:gravity="center" 
                android:background="@drawable/table_header_cell"
                android:textStyle="bold" android:textSize="13sp"/>
        <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/textViewBRH" android:textColor="@color/white" 
                android:layout_weight="1"
                android:gravity="center" 
                android:background="@drawable/table_header_cell"
                android:textSize="13sp" android:textStyle="bold"/>
        <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/textViewBWsH" android:textColor="@color/white"  
                android:layout_weight="1"               
                android:gravity="center" 
                android:background="@drawable/table_header_cell"
                android:textSize="13sp" android:textStyle="bold"/>
        <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/textViewBECOH" android:textColor="@color/white" 
                android:layout_weight="1"
                android:gravity="center" 
                android:background="@drawable/table_header_cell"
                android:textSize="13sp" android:textStyle="bold"/>

    </LinearLayout>


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

</LinearLayout>


</LinearLayout>

</ScrollView>
Bootle answered 13/5, 2013 at 6:18 Comment(5)
Android ListView that does not scroll? https://mcmap.net/q/714871/-android-listview-that-does-not-scroll/1012284Sulfonmethane
Scrolling with Multiple ListViews for Android https://mcmap.net/q/432728/-scrolling-with-multiple-listviews-for-android/1012284Sulfonmethane
You can see this post: #16332045Bookerbookie
@Ahmad Ali Nasir- this is too long....you should post the hierarchy form outline view..!Arel
UPDATE: Now Support Library allows to make it easier with <NestedScrollView> and <RecyclerView>. NestedScrollView can contains any layout with number of simple or recycler- views insidePaleoasiatic
C
8

It can be done, although you should not put a listview inside a scrollview but sometimes this is the easier way: https://mcmap.net/q/37081/-how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing

Curtcurtail answered 13/5, 2013 at 6:43 Comment(0)
R
25

Answer from a Google employee that works in Android:

Do not use ListView inside ScrollView

https://plus.google.com/107708120842840792570/posts/T5AgJ7jiK88

enter image description here

Royceroyd answered 13/5, 2013 at 6:24 Comment(0)
C
8

It can be done, although you should not put a listview inside a scrollview but sometimes this is the easier way: https://mcmap.net/q/37081/-how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing

Curtcurtail answered 13/5, 2013 at 6:43 Comment(0)
S
7

Use Like this: THIS WORKED LIKE CHARM

Remove Linear layout. use relative layout and inside that place your two list view like this.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollojt"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#f00"></ListView>

        <ListView
            android:id="@+id/listView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/listView1"
            android:background="#0f0"></ListView>
    </RelativeLayout>
</ScrollView>

add Utility.java

public class Utility {

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

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

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

In your Activity:

lv1.setAdapter(adapter);
lv2.setAdapter(adapter);

Utility.setListViewHeightBasedOnChildren(lv1);
Utility.setListViewHeightBasedOnChildren(lv2);

Thanks to #Nirmal for the actual answer https://mcmap.net/q/369331/-android-how-to-display-2-listviews-in-one-activity-one-after-the-other

Sayers answered 23/3, 2016 at 19:15 Comment(0)
P
2

ScrollView and ListView both use scrolling. So in my opinion they shouldn't be used together. Try using RelativeLayout or some other layout instead of ScrollView

Particularize answered 13/5, 2013 at 6:22 Comment(1)
I know that ScrollView and ListView should not be used together but its essential that I do. Reason being that if I don't then the ListViews scrolling is active instead of the complete View.Bootle
W
0

You can set it by engulfing ListView in a parent layout like an example below -

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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


        <androidx.cardview.widget.CardView
            android:id="@+id/cardBrand"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="@dimen/fab_margin"
            android:layout_below="@+id/cardRange"
            app:cardElevation="3dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/brandTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Brands"
                    android:layout_marginTop="@dimen/fab_margin"
                    android:layout_marginLeft="@dimen/fab_margin"
                    android:layout_marginRight="@dimen/fab_margin"
                    android:textAppearance="@style/TextAppearance.AppCompat.Small"
                    android:textColor="@color/md_black_1000"/>

                <ListView
                    android:id="@+id/brandList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginBottom="@dimen/fab_margin" />

            </LinearLayout>

        </androidx.cardview.widget.CardView>

        <Space
            android:layout_width="match_parent"
            android:layout_height="6dp" />

        <androidx.cardview.widget.CardView
            android:id="@+id/cardRating"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="@dimen/fab_margin"
            android:layout_below="@+id/cardBrand"
            app:cardElevation="3dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Ratings"
                    android:layout_marginTop="@dimen/fab_margin"
                    android:layout_marginLeft="@dimen/fab_margin"
                    android:layout_marginRight="@dimen/fab_margin"
                    android:textAppearance="@style/TextAppearance.AppCompat.Small"
                    android:textColor="@color/md_black_1000"/>

                <ListView
                    android:id="@+id/ratingList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginBottom="@dimen/fab_margin" />

            </LinearLayout>

        </androidx.cardview.widget.CardView>

        <Space
            android:layout_width="match_parent"
            android:layout_height="6dp" />

    </LinearLayout>

</androidx.core.widget.NestedScrollView>

And, after the listview is populated -

eachitemSize = 180;

cardBrand.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, eachitemSize * brandsAdapter.getCount()));

cardRating.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, eachitemSize * ratingAdapter.getCount()));

What we did above is, we set the ScrollView height to match_parent. And after the ListView is populated we defined each parent layout of ListView to its ListView item count. And, remember to change the value of eachitemSize according to your each cell size.

Another thing to care about is after the LayoutParams is assigned its margin becomes invalid due to the new LayoutParams. So, use <Space/> instead of margin like the example above.

Hope it helps!!

Wallin answered 3/7, 2019 at 8:46 Comment(0)
O
0

I would recommend to use listview with section headers ...i think that is what you want to implement...

This may be helpful https://learnlinky.com/2016/11/30/listview-section-header-android/

the answer given by reddy raz work only if we set fixed height of list items

Oldest answered 10/10, 2020 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.