Reputation: 708
I am using a Background Service to download some files and update the progress in the RecyclerView. when the progress is changed the service will use a Callback to the Activity with the item that changed with the progress. I want to know which one is Better in terms of Performance?
so do i have to do it like this using findViewHolderForAdapterPosition(position);
@Override
public void onProgressUpdate(String id, int pos, int progress) {
MyViewHolder vh = recyclerView.findViewHolderForAdapterPosition(pos);
vh.progressBar.setProgress(progress);
}
OR
@Override
public void onProgressUpdate(String id, int pos, int progress) {
adapter.getProgressHashmap().put(id, progress);
adapter.notifyItemChanged(pos);
}
and in my Adapter, I have a Hashmap
HashMap<String, Integer> progressHashmap = new HashMap<>();
now ,inside onBindViewHolder
I will check if the hashmap contains the id then update the ProgressBar
if (progressHashmap.containsKey(id)) {
int progress = progressHashmap.get(id);
holder.progressBar.setProgress(progress);
}
Upvotes: 2
Views: 563
Reputation: 261
I too would like to know more about this.
From my experience, obtaining the ViewHolder using findViewHolderForAdapterPosition(int position)
and updating only the data you need to update is more efficient than calling notifyItemChanged(int position)
, because the latter invalidates and redraws the entire view.
The alternative is to call notifyItemChanged(int position, Object payload)
, pass progress
as the payload and handle the progress update in onBindViewHolder(VH holder, int position, List<Object> payloads)
. I can give you more info about how to do this if you like.
I don't know which is the safest out of these methods, but if I had to guess it would be the latter because findViewHolderForAdapterPosition
returns null
if the RecyclerView is calculating its layout.
Upvotes: 1