当前位置:网站首页>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
边栏推荐
- 分屏bug 小记
- error LNK2019: 无法解析的外部符号
- 10 张图打开 CPU 缓存一致性的大门
- 服务器到服务器 (S2S) 事件 (Adjust)
- [learning notes] segment tree selection
- [dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours
- 记一次 .NET 某新能源系统 线程疯涨 分析
- 工具箱之 IKVM.NET 项目新进展
- Problems that cannot be accessed in MySQL LAN
- 10 pictures open the door of CPU cache consistency
猜你喜欢

Getting started with MySQL

日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则

Ogre introduction

详细介绍六种开源协议(程序员须知)

ESP32 ① 编译环境

1、深拷贝 2、call apply bind 3、for of for in 区别

分布式事务解决方案

Detr introduction

Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set

Ogre入门尝鲜
随机推荐
RecyclerView的数据刷新
MongoDB优化的几点原则
Problems that cannot be accessed in MySQL LAN
Storage principle inside mongodb
[learning notes] zkw segment tree
将数学公式在el-table里面展示出来
Mongodb meets spark (for integration)
Vscade editor esp32 header file wavy line does not jump completely solved
cmake 学习使用笔记(一)
Detr introduction
高端了8年,雅迪如今怎么样?
My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
DETR介绍
C语言数组相关问题深度理解
MongoDB 遇见 spark(进行整合)
[etc.] what are the security objectives and implementation methods that cloud computing security expansion requires to focus on?
Cinnamon 任务栏网速
OSI seven layer model
Milkdown 控件图标
JS中为什么基础数据类型可以调用方法