【问题标题】:disable listview scrolling inside scrollview禁用滚动视图内的列表视图滚动
【发布时间】:2017-05-17 06:42:07
【问题描述】:

我在滚动视图中实现两个列表视图时遇到问题。我有活动,其中有滚动视图。 这是我想要的图像

layout design

actually design

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

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

但我没有得到我想要的。

我怎样才能做到这一点?

【问题讨论】:

  • 发布布局的xml
  • 改用 NestedScrollView
  • 您能发布您的布局 XML 吗?

标签: android listview android-recyclerview scrollview


【解决方案1】:

不要在ScrollView 中使用ListView。

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

NestedScrollView 和ScrollView 一样,但它支持充当 nested 在新旧版本上滚动 parent 和 child 安卓的。默认启用嵌套滚动。

见documentation。

这是一个例子:

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

希望对你有所帮助~

【讨论】:

  • 我在更新的答案中添加了一个示例。如果我的答案看起来有用,您可以投票并标记为正确答案。在此先感谢:)
  • 哦知道了会在测试后通知你
  • @user7824858:你的测试进展如何,你现在能接受这个答案吗?
  • @user7824858 如果没有一个单一的解决方案可以解决你的问题,而不是发布你自己的答案,我认为你已经解决了
猜你喜欢
  • 1970-01-01
  • 2014-08-20
  • 1970-01-01
  • 1970-01-01
  • 2011-11-28
  • 1970-01-01
  • 2020-01-06
  • 2013-12-30
  • 1970-01-01
相关资源
最近更新 更多