嵌套的RecyclerView无法回收重用

4

我有一个非常复杂的UI,需要多个嵌套的列表,以此类推。

目前我有一个外部RecyclerView,里面有另一个RecyclerView,实际上是可扩展的Recycler(是这个组件的重构版本)。它是一个三层次的层次结构。

我的问题是没有任何东西被回收。它们不在NestedScrollView中。它们从创建时就会加载到内存中,onBindViewHolder永远不会再次触发...结果滚动非常粗糙。

有什么建议来解决这个问题吗?提前感谢。

外部recycler

<?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);
        }
    }

1
对于两个控件,它们具有相同的ID:@+id/rcv_live - Amjad Khan
不幸的是,那并没有做任何事情。 - Stamatis Stiliats
你尝试过在适配器中覆盖onViewRecycled()方法进行调试吗? - Gotiasits
是的。它从来没有触发过。在我的回收器中都没有。 - Stamatis Stiliats
你能否请检查这个答案?https://dev59.com/J-k6XIcBkEYKwwoYDPsR#39060431 - Reaz Murshed
1个回答

0

当子项高度设置错误时,就会出现这种情况。

只需将子项的layout_height设置为wrap_content即可解决问题。


我将项目高度(包括RecyclerView的高度,因为它也是一个子项)更改为wrap_content,但仍然无法回收利用。 - Stamatis Stiliats

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