RecyclerView嵌套在NestedScrollView中,onBindViewHolder会为所有getItemCount大小调用。

12
当我将 RecyclerView 放在 NestedScrollView 中时,就像说我有一个大小为30的列表,即使没有滚动,onBindViewHolder 也会一次性调用所有30行的绑定视图方法。
 RecyclerView list;
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        list.setLayoutManager(layoutManager);
        layoutManager.setAutoMeasureEnabled(true);
        list.setNestedScrollingEnabled(false);
        list.addItemDecoration(new VerticalSpaceItemDecoration(5));
        list.setAdapter(adapter);

我的 XML 是

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:scrollbars="none"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/grey">
    <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_views"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/info"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:textAlignment="center"

            android:visibility="visible"
           />

但如果我移除NestedScrollView,它可以正常工作。


你找到了解决这个问题的办法吗?这绝对与嵌套在NestedScrollView中的RecyclerView有关。 - Zain Ali
有没有解决这个问题的方法?我也遇到了同样的问题。 - thedarkpassenger
https://stackoverflow.com/a/37649336/1237141 这个答案对我也没有用。 - thedarkpassenger
4个回答

2
我假设您正在使用appbar_scrolling_view_behavior,那么您想要做的事情与AppBarLayout有关。如果是这样,您可以将RecyclerView作为CoordinatorLayout的直接子项,并支持AppBarLayout滚动,而无需将RecyclerView嵌套在NestedScrollView中。请尝试以下操作:在CoordinatorLayout中使用RecyclerView(带有AppBarLayout和CollapsingToolbarLayout):
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:background="#55FF00FF"
                app:layout_collapseMode="none"/>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

在您的Activity或CustomView中:

RecyclerView list;
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
list.setLayoutManager(layoutManager);
list.addItemDecoration(new VerticalSpaceItemDecoration(5));
list.setAdapter(adapter);

嗨,我有同样的问题。在我的情况下,我有一个FragmentContainerView,它加载包含RecyclerView的片段。我该如何解决这个问题? - hushed_voice

1
但是你将NestedScrollViewandroid:layout_height设置为wrap_content,这里默认值为零(因为在声明时没有内容)。 接下来,你将RecyclerView的android:layout_height设置为match_parent - 目前为止是0。 因此,所有项目的高度都为0。
因此,你有这样的情况。 解决方案:使用@dkarmazi上面的解决方案https://dev59.com/XFoU5IYBdhLWcg3wk3l2#37558761或尝试更改参数android:layout_height值。

1

这是正确的。因为您正在使用 ScrollViewScrollView 不像 RecyclerViewListView 一样具有可回收性。它会一次性显示所有包含在屏幕外的视图。您应该使用其他布局。


0

我遇到了同样的问题。经过一番研究,找到了解决方案。

您需要确保您的RecyclerView高度是固定的,通过将其设置为MATCH_PARENT。或者如果它在约束布局中,则将高度设置为0dp并设置所需的高度约束。然后将recyclerview.setHasFixedSize设置为true。

此后,onBindViewHolder将开始被调用。


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