ListView在NestedScrollView中无法展开

41

我在我的活动页面中使用CoordinatorLayout。 在应用栏下方有一个ListView。 但是,当我使用ListView而不是NestedScrollView时,它无法正常工作。如果我将ListView放入NestedScrollView中,则ListView不会扩展。


2
https://dev59.com/Yuo6XIcBkEYKwwoYSCpJ#6211286 - Sree
10个回答

73

当你添加android.support.v4.widget.NestedScrollView中的android:fillViewport="true"属性时,你就可以修复它了 :) 。这是我的代码。

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:fillViewport="true"
    >
    <ListView
        android:id="@+id/list_myContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        >
    </ListView>

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

26
我也遇到了同样的问题。ListView 的内容无法滚动。 - Luis E. Fernandez
请问您的 API 级别是多少? - Alpha Huang
6
Listview无法滚动。 - piyush poriya
你和我一起工作,非常感谢你,你真的节省了我的时间。只是添加了标签android:fillViewport="true"。 - Waleed A. Elgalil
4
它确实扩大了视口,但我无法滚动。 - Keno
显示剩余4条评论

51

从棒棒糖版本开始,您可以使用

setNestedScrollingEnabled(true);

在您的ListView/GridView/ScrollableView上启用或禁用嵌套滚动。来自文档

为此视图启用或禁用嵌套滚动

如果您需要与旧版操作系统向后兼容,则必须使用RecyclerView。您可以在此处了解更多信息。

编辑。 ViewCompat具有静态方法setNestedScrollingEnabled(View,boolean)。例如:

ViewCompat.setNestedScrollingEnabled(listView, true)

感谢@Dogcat指出此问题。


2
这个答案应该被点赞并且现在设置为正确答案。我最终改变了我的实现方式,使用了RecyclerView,因为我只阅读了被接受的答案。是的,当然我可以阅读所有的答案,但第一个对我有效 - 只是更改实现方式有点麻烦 ;-) - Darwind
1
你刚刚救了我!是的,这应该是被采纳的答案。 - liltof
不行,它不能工作。文档中提到:“如果此视图未实现嵌套滚动,则此操作将无效。” - k4dima
2
ViewCompat在Android Lolipop之前没有任何作用:https://dev59.com/no_ea4cB1Zd3GeqPPohX - Hrk

35
为了让 CoordinatorLayout 正常工作,您需要使滚动子视图实现NestedScrollingChild。这种类包括NestedScrollViewRecyclerView
简单来说 - 只需使用RecyclerView作为滚动内容,它就会正确地工作 :)
另外,我认为您再也没有理由使用ListView了。我知道这是一种习惯,并且设置起来更加容易(因为您已经做过很多次),但是使用RecyclerView是推荐的方式。

1
我还没有看到RecyclerView的快速滚动和分区适配器实现。 - sha
1
如何实现可扩展的列表视图? - Sagar Panwala
7
RecyclerView 无法处理 CursorAdapter,这可能是一个很好的原因! - Blodhgard
更多关于是否应该用RecyclerView替换ListView的细节,请参考以下链接: https://dev59.com/G14c5IYBdhLWcg3wCGf2 - Ivan Milisavljevic
@Dogcat 不是这样的。文档中写道:“如果此视图未实现嵌套滚动,则此操作将无效。” - k4dima
显示剩余3条评论

15

这是对我有用的方法。

NestedScrollView 上设置 android:fillViewport="true"

将一个布局元素作为 NestedScrollView 的子元素添加。在我的情况下是 LinearLayout,然后在 ListView 上设置 android:nestedScrollingEnabled="true",让 ListView 成为 LinearLayout 的子元素。

就可以了


1
我之前也想说同样的话,直到我看到了这个。这个解决方案适用于API 21及以上。 - zuko
只有这个解决方案对我有效。使用LinearLayout有助于在外部NestedScrollView中嵌套组合UI。谢谢! - JWL

10
只需在你的NestedScrollView标签中添加android:fillViewport="true"即可。

请解释一下您的答案是如何解决问题的,这将有助于大家更清楚地了解您的解决方案,并为将来的参考留下记录。 - Aziz
@Aziz,实际上我也遇到了同样的问题,而且它起作用了。将“fillViewport”设置为true后,它会将内容的高度拉伸到视口的边界。 - Shivam

9

你的列表视图将会滚动。希望有所帮助。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    <android.support.v4.widget.NestedScrollView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:fillViewport="true"
         app:layout_behavior="@string/appbar_scrolling_view_behavior">

         <ListView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:nestedScrollingEnabled="true">
         </ListView>
    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

4

Below code worked for me:

ViewCompat.setNestedScrollingEnabled(listView, true);

您的ListView应该在NestedScrollView


1
如果可能的话,请用RecyclerView替换你的ListView,否则创建自定义ListView并将ListViewonMeasure设置为以下内容:
 public NonScrollListView(Context context) {
        super(context);
    }

    public NonScrollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }

这个 ListView 将不再能够滚动,可以在 NestedScrollView 中使用。


0

在嵌套滚动视图中无法滚动列表视图。请使用带有嵌套滚动视图的RecyclerView。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:orientation="horizontal">

        <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/profile_image"
            android:layout_width="76dp"
            android:layout_height="76dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="24dp"
            android:layout_marginStart="24dp"
            android:src="@drawable/profile"
            app:border_color="#FF000000" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/profile_image"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="IRFAN QURESHI"
                android:textSize="20sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="irfan123@gmail.com" />
        </LinearLayout>

        <ImageView
            android:layout_marginLeft="50dp"
            android:layout_gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_delete_black" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"

        android:background="@color/colorPrimary"
        android:gravity="center_horizontal"
        android:padding="30dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/login_email_bg_round_rect_shape"
            android:gravity="center_horizontal"
            android:padding="10dp"
            android:text="POST A QUERY" />
    </LinearLayout>

        <!--<ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </ListView>-->

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />


    <RelativeLayout
        android:background="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:padding="8dp"
            android:gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SIGN OUT" />
        <ImageView
            android:paddingTop="5dp"
            android:layout_marginRight="40dp"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_delete_black" />
    </RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>

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

0
只需要在您的NestedScrollView中添加android:nestedScrollingEnabled="true"标签即可。
<android.support.v4.widget.NestedScrollView
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:scrollbars="none"
  android:nestedScrollingEnabled="true">
   <ListView
      android:id="@+id/list_myContent"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:scrollbars="vertical">
  </ListView>


1
这个仅支持API 21及以上版本。不幸的是,我最低要求构建16。 - Keno

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接