I have a item in RecyclerView
which does not need to be updated when I call notifyDataSetChanged
.
But I didn't find any methods to make the specified item avoid updated. Is it possible to do this ?
I need to do this because the item is a WebView
, it had loaded a html page, if I called notifyDataSetChanged
, this item will flash.
The reason why I post this question is avoiding the flash of the WebView
when notifyDataSetChanged
(see this quesion ). After the failure of using notifyItemRangeXXX
methods, I post this question.
But after checking your answers and comments, it seems that it's impossible to avoid updating when using notifyDataSetChanged
.
The purpose of notifyDataSetChanged
IS to update ALL items. Don't use it if you don't want this behavior!
The correct approach would be to only update the data you've changed. There are some methods which will help you to only invalidate the desired data:
notifyItemChanged(int position)
notifyItemInserted(int position)
notifyItemMoved(int fromPosition, int toPosition)
notifyItemRangeChanged(int positionStart, int itemCount)
notifyItemRangeRemoved(int positionStart, int itemCount)
notifyItemRemoved(int position)
Yes, its actually possible to do so. notifyDataSetChanged()
notifies the RecyclerView that all the elements in the data set has changed .
As you do not want that to happen and exclude specific items.
for(int i = 0; i< mAdapter.getItemCount(); i++){
if(i != itemPositionToExclude){
mAdapter.notifyItemChanged(i);
}
}
So, we need to loop through all the elements in the adapter and call notifyItemChanged()
only for those positions which you do not want to exclude
.
You should think in different way - which items need to be updated. To update UI of item, after data has changed use method notifyitemchanged
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.