【问题标题】:RecyclerView inside NestedScrollView onBindViewHolder calling for all getItemCount sizeNestedScrollView onBindViewHolder 内的 RecyclerView 调用所有 getItemCount 大小
【发布时间】:2016-05-19 11:38:00
【问题描述】:

当我将RecyclerView 放入NestedScrollView 中时,onBindViewHolder 会调用所有行,比如说我有一个大小为 30 的列表,然后即使不滚动,也会一次调用所有 30 行的onBindViewHolder

 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里面的RecylerView有关
  • 这个问题有什么解决办法吗?面临同样的问题。
  • stackoverflow.com/a/37649336/1237141 答案也对我不起作用。

标签: android android-recyclerview nestedscrollview android-nestedscrollview


【解决方案1】:

我假设由于您使用的是 appbar_scrolling_view_behavior,因此您正在尝试使用 AppBarLayout 做一些事情。

如果是这样,您可以将 RecyclerView 用作 CoordinatorLayout 的直接子级,并支持 AppBarLayout 滚动,而无需在 NestedScrollView 内嵌套 RecyclerView。

试试这个:RecyclerView inside CoordinatorLayout(带有 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 的片段。我该如何解决这个问题?
【解决方案2】:

但是您将 NestedScrollViewandroid:layout_height 设置为 wrap_content - 这里默认为零(因为在声明的时刻)。接下来,对于 RecyclerView,您将 android:layout_height 设置为 ma​​tch_parent - 目前为 0。因此,所有项目的高度均为 0。

因此,您遇到了这种情况。 解决方案:使用上面来自@dkarmazi https://stackoverflow.com/a/37558761/3546306 的解决方案或尝试更改参数 android:layout_height 值。

【讨论】:

    【解决方案3】:

    没错。因为您使用的是ScrollViewScrollView 不像RecyclerViewListView 那样可回收。它会一次性显示所有包含这些的视图。您应该使用其他布局而是。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-07
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      相关资源
      最近更新 更多