Multiple ListViews inside a ScrollView

2019-02-02 21:53发布

问题:

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>

回答1:

It can be done, although you should not put a listview inside a scrollview but sometimes this is the easier way: https://stackoverflow.com/a/3495908/1117338



回答2:

Answer from a Google employee that works in Android:

Do not use ListView inside ScrollView

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



回答3:

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://stackoverflow.com/a/17693628/1403112



回答4:

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