【问题标题】:Nested RecyclerView doesn't recycle嵌套的 RecyclerView 不回收
【发布时间】:2018-10-14 22:31:00
【问题描述】:

我有一个非常复杂的 UI,需要多个嵌套列表等。

目前我有一个外部RecyclerView,内部还有另一个RecyclerView,它实际上是一个可扩展的回收器(是这个component 的重构版本)。它是一个三层结构。

我的问题是没有东西被回收。 NestedScrollView 里面没有它们。它们从创建的那一刻起就被加载到内存中,onBindViewHolder 再也不会触发了。因此滚动非常粗糙。

关于如何解决这个问题的任何建议?提前致谢

外部回收器

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:background="@color/color_background_expandable_list">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:contentInsetStart="0dp">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rcv_bet_sports"
                android:scrollbars="none"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#3D464F" />
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rcv_live"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:descendantFocusability="blocksDescendants"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

内部可扩展回收器

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="wrap_content">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rcv_live"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

更新 这是主要的外部适配器。我覆盖 GetItemViewId。这会影响回收机制吗?

class LivePageAdapter : RecyclerView.Adapter
    {
        LiveContentPresenter _ContentPresenter;
        RecyclerView.RecycledViewPool _SharedPool = new RecyclerView.RecycledViewPool();

        public LivePageAdapter(LiveContentPresenter contentPresenter)
        {
            _ContentPresenter = contentPresenter;
        }

        public void AddWidget(PageWidgetModel widgetModel)
        {
            _ContentPresenter.Add(widgetModel);
        }

        public override int ItemCount => _ContentPresenter.WidgetCount;

        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            _ContentPresenter.BindWidgetOnRow((LiveListViewHolder)holder, position);
        }

        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            var type = (WidgetType)viewType;
            switch (type)
            {
                case WidgetType.Carousel:
                    return null;
                case WidgetType.HotRightNow:
                    return null;
                case WidgetType.ExpandableRecycler:
                    View liveListItem = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.widget_row_live_list, parent, false);
                    return new LiveListViewHolder(liveListItem, _SharedPool);
                default:
                    return null;
            }
        }

        public override long GetItemId(int position)
        {
            return _ContentPresenter.WidgetModels[position].GetHashCode();
        }

        public override void OnViewRecycled(Java.Lang.Object holder)
        {
            base.OnViewRecycled(holder);
        }

        public override int GetItemViewType(int position)
        {
            return _ContentPresenter.GetWidgetType(position);
        }
    }

【问题讨论】:

  • 因为你们都有相同的 id @+id/rcv_live
  • 很遗憾什么都没做
  • 您是否尝试过覆盖适配器中的onViewRecycled() 进行调试?
  • 是的。而且它永远不会着火。我的回收站没有
  • 你能在这里检查这个答案吗? stackoverflow.com/a/39060431/3145960

标签: android xamarin android-recyclerview xamarin.android nestedrecyclerview


【解决方案1】:

当孩子的身高不正确时会发生这种情况。

只需将子项的layout_height 设置为wrap_content 即可修复它。

【讨论】:

  • 我将项目高度(包括 recyclerview 的高度,因为这也是一个孩子)更改为 wrap_content 但仍然没有回收
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 2020-03-08
  • 1970-01-01
相关资源
最近更新 更多