ListView 未在 NestedScrollView 内扩展

ListView not expanding inside NestedScrollView

我在 activity 页面中使用 CoordinatorLayout。因为在应用栏下方有 ListView。但是当我使用 ListView 而不是 NestedScrollView 时它不起作用。如果我把 ListView 放在 NestedScrollView 里面,ListView 就不会展开

要使 CoordinatorLayout 正常工作,您需要滚动子项来实现 NestedScrollingChild。这样的 类 是 NestedScrollViewRecyclerView.

简而言之 - 只需对滚动内容使用 RecyclerView 即可正常工作:)

P.S。作为旁注,我看不出您为什么会再使用 ListView 的原因。我知道这是一种习惯,而且更容易设置(因为你已经做过很多次了),但无论如何使用 RecyclerView 是推荐的方式。

您可以在 android.support.v4.widget.NestedScrollView 中添加 addtribute 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>

从 Lollipop 开始,您可以使用

setNestedScrollingEnabled(true);

在你的 ListView/GridView/ScrollableView 上。来自文档

Enable or disable nested scrolling for this view

如果您需要向后兼容旧版本的 OS,则必须使用 RecyclerView。你可以阅读更多here

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

ViewCompat.setNestedScrollingEnabled(listView, true)

感谢 @Dogcat 指出

只需将 android:fillViewport="true" 放入您体内 NestedScrollView 标签

你不能在 nestedscrollview.Use 带有 nestedscrollview

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

只需在 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>

这对我有用。

NestedScrollView

上设置 android:fillViewport="true"

将一个布局元素添加为 Child 到 NestedScrollView。在我的例子中 LinearLayout 然后 在 ListView 上设置 android:nestedScrollingEnabled="true" 使 ListView 成为 LinearLayout 的 child

干得好

以下代码对我有用:

ViewCompat.setNestedScrollingEnabled(listView, true);

你的ListView应该在NestedScrollView

里面

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

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

如果可能,请将您的 ListView 替换为 RecyclerView,否则创建您的 customListView 并将 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 中使用。