简体   繁体   中英

Android change textview in recyclerview in all items without redrawing the entire list

I have a recyclerview with a list of items, and each item has, among other things, a textview with a distance from the user. As the user physically moves around I want to update the distances of the items without redrawing the list. For example, assume the user is standing in the middle of a triangle with the points being item1,2,3:

item1 5 feet

item2 10 feet

item3 15 feet

now the user moves 2 feet towards item1 - so it changes to:

item1 3 feet

item2 12 feet

item3 17 feet

but I don't want a redraw. In other words, if the list has 5000 items and the user has scrolled down to item 3150 and then changed his location, I don't want to do something like notifydatasetchanged which will then redraw the entire list and show the user everything from position 0. I want to instead dynamically change only the text for the distance for every item in the list and, as far as the user is concerned, everything is the same and only the distances have changed.

What is the best way of doing this?

First of all, you cannot ovoid list redraw, that is just how it works to update UI but rest assured, there are caches all around to handle small changes.

About items, if you call notifyDataSetChanged RecyclerView will rebind only visible views. So that will be the cost you are paying, the actual size of the adapter (5000 in your example) is irrelevant.

If you want RecyclerView to update only changed views, you have to call notifyItemChanged(position) . In that case, RecyclerView will rebind only changed items and only if they are visible.

If you have stable ids, even if you call notifyDataSetChanged , if your adapter has stable ids, RecyclerView will call onBindViewHolder with the existing view holder that currently represents the item at that position. Keep a reference to your item in the ViewHolder. When onBind is called, check if it is the same item and only update necessary parts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM