禁用列表视图在scrollview中滚动 [英] disable listview scrolling inside scrollview

查看:101
本文介绍了禁用列表视图在scrollview中滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在滚动视图中实现两个listview时遇到了问题.我有可以进行滚动浏览的活动. 这是我想要的图像

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

布局设计

实际上是设计

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

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.

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

I tried recyclerview and with this property

note_recyclerview.setNestedScrollingEnabled(false);

但是我没有得到想要的东西.

but I didn't get what I want.

我该如何实现?

推荐答案

请勿在ScrollView内使用ListView.

Do not use ListView inside ScrollView.

由于您使用多个ListView,因此应使用android.support.v4.widget.NestedScrollView而不是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就像ScrollView一样,但是它支持充当 在新版本和旧版本上都同时滚动nestedparent Android.默认情况下,嵌套滚动处于启用状态.

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.

请参见文档.

以下是示例:

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

希望这会有所帮助〜

这篇关于禁用列表视图在scrollview中滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆