繁体   English   中英

禁用列表视图在scrollview中滚动

[英]disable listview scrolling inside scrollview

我面临在滚动视图中实现两个listview的问题。 我有可进行滚动浏览的活动。 这是我想要的图像

版图设计

实际设计

我要制作包含两个列表视图的发票,一个用于项目,一个用于跟踪数据。 我能够动态地使listview高度,也可以禁用其click事件。 但是现在在列表视图中,我无法单击或滚动屏幕。 所有组件都在滚动视图中。 但是当我触摸列表视图时,我无法滚动滚动视图。

这是我在管理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;
    }

}`

我尝试了recyclerview并使用此属性

note_recyclerview.setNestedScrollingEnabled(false);

但我没有得到我想要的。

我该如何实现?

不要在ScrollView使用ListView

当您使用多个ListView ,因此应使用android.support.v4.widget.NestedScrollView而不是ScrollView来获得正确的滚动行为。

NestedScrollView就像ScrollView一样,但是在新旧版本的Android上,它都同时充当nested滚动parentchild 默认情况下启用嵌套滚动。

请参阅文档

这是一个例子:

<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>

希望这会有所帮助〜

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM