Reputation: 775
I use the StaggeredGridLayoutManager as the RecyclerView layout manager. 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.
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!
Upvotes: 1
Views: 2839
Reputation: 3140
@ErShani gave a valuable insight to the problem and I am adding more on top of it.
Use notifyDataSetChanged() as a last resort when using recycler view. notifyDataSetChanged() does the following and I am quoting from the doc
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.
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.
So in your case if you call notifyDataSetChanged(), the already bound and visible layouts are also rebound which is pointless and ineffective.
You could use notifyItemRangeInserted() after loading another page. This is more effective and retains default animations provided by RecyclerView on data set changes.
Upvotes: 2
Reputation: 392
RecyclerView does not do any thing with data. it just get from your database by network service or from sqlite.
whichever data order you will provide, it will accept it as it is. so no sorting is done by recyclerview.
then also you can check which child element of recyclerview is drawn first and which one drawn later by implementing onChildDrawingOrder listener.
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. so you can try,
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
Upvotes: 1