当前位置:网站首页>Data refresh of recyclerview
Data refresh of recyclerview
2022-07-07 13:29:00 【The ancient moon cannot be seen by modern people. This moon onc】
Preface
In writing today Demo When , See the official right notifyDataSetChanged() A passage described like this :
Key content
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.
Officials say that if you are using an adapter , You can use a more specific change event to achieve higher efficiency , hold notifyDataSetChanged() As a last resort .
So , There are just a few ways :
Key content :
notifyItemChanged(int)
notifyItemInserted(int)
notifyItemRemoved(int)
notifyItemRangeChanged(int, int)
notifyItemRangeInserted(int, int)
notifyItemRangeRemoved(int, int)
These six methods , yes RecyclerView Peculiar , about ListVIew There are no such methods , Let's explore these six methods one by one , First look at the official description :
notifyItemChanged(int)
Notify any registered observers that the item at position has changed.Equivalent to calling notifyItemChanged(position, null);
This is an item change event, not a structural change event. It indicates that any reflection of the data at position is out of date and should be updated.
The item at position retains the same identity.
( Poor translation of Chinese :) Remind any registered observer , This item stay position The position has changed , This is equivalent to notifyItemChanged(position, null), This is a item Changing events , It is not an event of structural change , It implies that any data reflected from this location has expired , Should be updated , In these positions item Your identity will not change ( Guess is the real object ).
notifyItemRemoved(int)
Notify any registered observers that the item previously located at position has been removed from the data set. The items previously located at and after position may now be found at oldPosition - 1.
This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.
( Poor translation of Chinese :) Remind any registered observer , This was before position In position item Has been removed from the data set , It was in position After that item Now it may be oldPosition-1 Find .
This is a structural change event , This represents something else that exists item It will still be considered the latest in this data set , This does not trigger rebinding , Although their position It may have changed .
notifyItemInserted(int)
Notify any registered observers that the item reflected at position has been newly inserted. The item previously at position is now at position position + 1.
This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.
This meaning is similar to the above , No translation .
The other three are scope changes , It means the same thing , Through looking at the source code found , The above three methods actually call the following three corresponding methods , Just put range Write for 1.
After reading the official description , I went to fight with the official description demo, After several waves of practice , Summarized below :
1: The official said structural change events and item What does it mean to change events ?
We all know , Whether it's listView still RecyclerView, Are highly customized ViewGroup, We added one by one item, In fact, it is one by one view, Arrange among them , For what officials call structural change events , Refer to item Increase or decrease , Because it involves other children view Re layout and drawing , So officials think this is a structural change event .
And the official description item Change events , It means just because of this item It doesn't correspond to the data in the data set ,item The displayed data has expired , It should be updated , and item The position of will not change , It will not trigger re layout and drawing .
2:notifyItemChanged(int) and notifyItemInserted(int) ,notifyItemRemoved(int) What's the difference ? They trigger onBindViewHolder Do you ?
As the official description above , It turns out that notifyItemInserted(int) ,notifyItemRemoved(int), It doesn't trigger onBindViewHolder, Because he is just a structural change event , Just put position In position item Delete or insert a , Others already exist item For data sets , Still considered to be the latest , Although their position It has changed , So they won't touch onBindViewHolder,
So for notifyItemChanged(int) Come on , I also verified the official statement , This method is considered to be in any position from position The mapped data is out of date relative to the data set , Need to be updated , but
It's in every position item The instance object of does not change , Therefore, the corresponding position will trigger onBindViewHolder Rebinding data .
Through the understanding of the above official description , It can also explain why the authorities recommend you to write an adapter , Using some more specific change events will be more efficient , Finally, post another paragraph notifyDataSetChanged() The official description of :
Notify any registered observers that the data set has changed.
There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred. Structural changes are when items are inserted, removed or moved within the data set.
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.
RecyclerView will attempt to synthesize visible structural change events for adapters that report that they have stable IDs when this method is used. This can help for the purposes of animation and visual object persistence but individual item views will still need to be rebound and relaid out.
No translation , Ha ha ha ha ……..
————————————————
Reproduced in :https://blog.csdn.net/DJH2717/article/details/81075639
边栏推荐
- Introduce six open source protocols in detail (instructions for programmers)
- [1] ROS2基础知识-操作命令总结版
- PAcP learning note 1: programming with pcap
- Analysis of DHCP dynamic host setting protocol
- 工具箱之 IKVM.NET 项目新进展
- User management summary of mongodb
- RecyclerView的数据刷新
- [QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)
- 一文读懂数仓中的pg_stat
- JS slow motion animation principle teaching (super detail)
猜你喜欢
Scripy tutorial classic practice [New Concept English]
Vscade editor esp32 header file wavy line does not jump completely solved
10 pictures open the door of CPU cache consistency
QQ medicine, Tencent ticket
PAcP learning note 1: programming with pcap
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
Analysis of DHCP dynamic host setting protocol
ESP32构解工程添加组件
MySQL入门尝鲜
JS slow motion animation principle teaching (super detail)
随机推荐
Introduce six open source protocols in detail (instructions for programmers)
MongoDB复制(副本集)总结
JS function returns multiple values
shell 批量文件名(不含扩展名)小写改大写
【学习笔记】zkw 线段树
JS判断一个对象是否为空
Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
Show the mathematical formula in El table
自定义线程池拒绝策略
工具箱之 IKVM.NET 项目新进展
PACP学习笔记三:PCAP方法说明
Some principles of mongodb optimization
Cookie and session comparison
详细介绍六种开源协议(程序员须知)
MATLAB中polarscatter函数使用
Navicat运行sql文件导入数据不全或导入失败
xshell连接服务器把密钥登陆改为密码登陆
Simple and easy-to-use code specification
如何让join跑得更快?
MongoDB的用户管理总结