当前位置:网站首页>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
边栏推荐
- Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
- 单片机原理期末复习笔记
- ROS机器人更换新雷达需要重新配置哪些参数
- Unity build error: the name "editorutility" does not exist in the current context
- ORACLE进阶(五)SCHEMA解惑
- Why can basic data types call methods in JS
- Shell batch file name (excluding extension) lowercase to uppercase
- 1. Deep copy 2. Call apply bind 3. For of in differences
- 一文读懂数仓中的pg_stat
- Pay close attention to the work of safety production and make every effort to ensure the safety of people's lives and property
猜你喜欢
Scrapy教程经典实战【新概念英语】
Scripy tutorial classic practice [New Concept English]
自定义线程池拒绝策略
ESP32构解工程添加组件
Talk about pseudo sharing
Getting started with MySQL
Error lnk2019: unresolved external symbol
LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)
存储过程的介绍与基本使用
Esp32 construction engineering add components
随机推荐
Ogre入门尝鲜
QQ的药,腾讯的票
Milkdown control icon
Analysis of DHCP dynamic host setting protocol
Cinnamon taskbar speed
Talk about pseudo sharing
MongoDB优化的几点原则
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
JNA学习笔记一:概念
迅为iTOP-IMX6ULL开发板Pinctrl和GPIO子系统实验-修改设备树文件
Mongodb command summary
PAcP learning note 3: pcap method description
[1] ROS2基础知识-操作命令总结版
LeetCode_二分搜索_中等_153.寻找旋转排序数组中的最小值
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
Clion mingw64 Chinese garbled code
10 张图打开 CPU 缓存一致性的大门
Summary of import, export, backup and recovery of mongodb
抓细抓实抓好安全生产各项工作 全力确保人民群众生命财产安全
mysql 局域网内访问不到的问题