disable listview scrolling inside scrollview

2019-08-17 19:38发布

问题:

I am facing problem to implement two listview in scrollview. I have activity in which I have scrollview. here is image what I want

layout design

actually design

I want to make invoice which contain two listviews one for items and one for tracking data. I am able to make listview height dynamically and also disable its click event. but now on listview I am not able to click or scroll screen. all components are in scrollview. but I am not able to scroll scrollview when I touch on listviews.

here is code where I'm managing height of listview

     public static boolean setListViewHeightBasedOnItems(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter != null) {

        int numberOfItems = listAdapter.getCount();

        // Get total height of all items.
        int totalItemsHeight = 0;
        for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
            View item = listAdapter.getView(itemPos, null, listView);
            float px = 500 * (listView.getResources().getDisplayMetrics().density);
            item.measure(View.MeasureSpec.makeMeasureSpec((int)px, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            totalItemsHeight += item.getMeasuredHeight();
        }

        // Get total height of all item dividers.
        int totalDividersHeight = listView.getDividerHeight() *
                (numberOfItems - 1);
        // Get padding
        int totalPadding = listView.getPaddingTop() + listView.getPaddingBottom();

        // Set list height.
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalItemsHeight + totalDividersHeight + totalPadding;
        listView.setLayoutParams(params);
        listView.requestLayout();
        return true;

    } else {
        return false;
    }

}`

I tried recyclerview and with this property

note_recyclerview.setNestedScrollingEnabled(false);

but I didn't get what I want.

how can I achieve this?

回答1:

Do not use ListView inside ScrollView.

As you are using multiple ListView, so you should use android.support.v4.widget.NestedScrollView instead of ScrollView to get proper scrolling behavior.

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

See documentation.

Here is an example:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <ListView
            android:id="@+id/listview1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </ListView>

        <ListView
            android:id="@+id/listview2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </ListView>
    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

Hope this will help~