简体   繁体   English

如何在notifyDataSetChanged时防止RecyclerView重新使用

[英]How to prevent the RecyclerView from resorting while notifyDataSetChanged

I use the StaggeredGridLayoutManager as the RecyclerView layout manager. 我使用StaggeredGridLayoutManager作为RecyclerView布局管理器。 When I try to load the next page data and then call notifyDataSetChanged method to update data, while I scroll the RecyclerView, I notice that I will resort my item. 当我尝试加载下一页数据,然后在滚动RecyclerView的同时调用notifyDataSetChanged方法更新数据时,我注意到我将重新使用我的项目。

I really don't need to resort it, how I can do to prevent it from resorting. 我真的不需要求助于该如何防止它求助。 And I wonder why it will resort. 我不知道为什么会诉诸于此。

Thx! 谢谢!

@ErShani gave a valuable insight to the problem and I am adding more on top of it. @ErShani对这个问题提供了宝贵的见解,我将在此之上添加更多内容。

Use notifyDatasetChanged() as a last resort when using recycler view. 使用回收站视图时,请使用notifyDatasetChanged()作为最后的手段。 notifyDatasetChanged() does the following and I am quoting from the doc notifyDatasetChanged()执行以下操作,我引用了该文档

This event does not specify what about the data set has changed, forcing any observers to assume that all existing items and structure may no longer be valid. 此事件未指定数据集已更改的内容,从而迫使所有观察者假定所有现有项目和结构可能不再有效。 LayoutManagers will be forced to fully rebind and relayout all visible views. LayoutManager将被迫完全重新绑定并重新布局所有可见视图。

If you are writing an adapter it will always be more efficient to use the more specific change events if you can. 如果您正在编写适配器,那么如果可以的话,使用更具体的更改事件将总是更加高效。 Rely on notifyDataSetChanged() as a last resort. 万不得已时,请依赖notifyDataSetChanged()。

So in your case if you call notifyDatasetChanged(), the already bound and visible layouts are also rebound which is pointless and ineffective. 因此,在您的情况下,如果调用notifyDatasetChanged(),则已经绑定和可见的布局也会反弹,这是毫无意义且无效的。

You could use notifyItemRangeInserted() after loading another page. 您可以在加载另一个页面后使用notifyItemRangeInserted() This is more effective and retains default animations provided by RecyclerView on data set changes. 这更有效,并保留RecyclerView提供的有关数据集更改的默认动画。

RecyclerView does not do any thing with data. RecyclerView对数据不做任何事情。 it just get from your database by network service or from sqlite. 它只是通过网络服务或sqlite从数据库中获取。

whichever data order you will provide, it will accept it as it is. 无论您提供的数据顺序是什么,它都会原样接受。 so no sorting is done by recyclerview. 因此recyclerview不会进行排序。

then also you can check which child element of recyclerview is drawn first and which one drawn later by implementing onChildDrawingOrder listener. 然后您还可以通过实现onChildDrawingOrder侦听器来检查先绘制recyclerview的哪个子元素,然后再绘制哪个子元素。

    recyclerView.setChildDrawingOrderCallback(new RecyclerView.ChildDrawingOrderCallback() {
            @Override
            public int onGetChildDrawingOrder(int childCount, int position) {
               // put log here 
               return position;
            }
        });

sorting or resorting is up to Layout Manager you attetch with recyclerview. 排序或重新排序取决于您使用recyclerview获取的布局管理器。 so you can try, 所以你可以尝试

layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在notifyDataSetChanged之后阻止RecyclerView中的自动滚动? - How to prevent auto scroll in RecyclerView after notifyDataSetChanged? 如何从另一个类通知RecyclerView的DataSetChanged? - How to notifyDataSetChanged for RecyclerView from another class? 如何更新RecyclerView-notifyDataSetChanged() - How to Update RecyclerView - notifyDataSetChanged() 如何在 notifyDataSetChanged 上为 recyclerView 设置动画? - How to animate recyclerView on notifyDataSetChanged? RecyclerView notifydatasetchanged 未从片段调用 - RecyclerView notifydatasetchanged not invoked from fragment 嵌套的 RecyclerView。 如何防止父 RecyclerView 在子 RecyclerView 滚动时滚动? - Nested RecyclerView. How to prevent parent RecyclerView from getting scrolled while child RecyclerView is scrolling? notifyDataSetChanged()从外部类到recyclerView - notifyDataSetChanged() to the recyclerView from an outer class RecyclerView中的notifyDatasetChanged在Fragment中不起作用 - notifyDatasetChanged in RecyclerView not working from Fragment 如何在recyclerview gridlayout中使用notifydatasetchanged - How to use notifydatasetchanged in recyclerview gridlayout 如何在用户滚动时防止RecyclerView单元的子级移动 - How to prevent a child of a RecyclerView cell from moving while the user scrolls
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM