当前位置:网站首页>Recyclerview refreshes blinks and crashes when deleting items
Recyclerview refreshes blinks and crashes when deleting items
2022-06-29 09:57:00 【Maple forest dream】
RecyclerView Refresh blink and delete Item Crash problem
/**
* todo RecyclerView Refresh blink Applicable to refresh single or multiple Item
* Problem cause analysis :
* RecyclerView There is one RecyclerViewPool Used to cache created item, Do not manually create RecyclerViewPool when , The system will automatically create a size of 5
* Of RecyclerViewPool.
* That means RecyclerView Of item The number is greater than 5, Each refresh will automatically call onCreateViewHolder().
* The newly created item If relevant content is not configured , The default values in the layout file... Are displayed . This causes flashing with and without data .
* <p>
* Problem solution :
* Manually create to RecyclerViewPool object , Specify its size , Again to RecyclerView object .
* <p>
* source :https://blog.csdn.net/u011433287/article/details/80859279
*
* @param view
*/
public static void noShank(RecyclerView view) {
RecyclerView.RecycledViewPool pool = new RecyclerView.RecycledViewPool();
pool.setMaxRecycledViews(0, 8);
view.setRecycledViewPool(pool);
}
/**
* todo RecyclerView Refresh blink Applicable to refresh single or multiple Item
* Problem cause analysis :
* RecyclerView Downhill sliding 、 When adding new data , The default animation method will be automatically adjusted every time , It is expressed as a picture
* from 0 -> 1 Set it up again , This will lead to the whole RecyclerView Show me from nothing , This causes a flashing condition
* <p>
* Problem solution :
* Turn off by setting RecyclerView Animation effect of
* <p>
* source :https://blog.csdn.net/d38825/article/details/86593843
*
* @param view
* @param adapter
*/
public static void noShank(RecyclerView view, CommonAdapter adapter) {
RecyclerView.ItemAnimator animator = view.getItemAnimator();
if (animator instanceof SimpleItemAnimator) {
((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
}
view.getItemAnimator().setChangeDuration(0);
adapter.setHasStableIds(true);
}
/**
* todo RecyclerView Refresh blink Applicable to refresh single Item
* Problem cause analysis :
* because notifyDataSetChanged(); The whole is refreshed RecyclerView, And we just need to brush a single Item nothing more
* <p>
* Problem solution :
* Use notifyItemChanged(int position); Refresh the specified location Item
*
* @param adapter
* @param position
*/
public static void noShank(CommonAdapter adapter, int position) {
adapter.notifyItemChanged(position);
}
**
* Delete the data at the specified index and refresh Item
*/
public void deleteIndexData(int position) {
// When users quickly click delete, they may be in Item When only two are left, an index exception is reported , as a result of position=1 Situated Item The delete operation has been performed
// But there is no such thing as to delete it , At this time, the data size is 1, So there's an error .
// The solution is to judge when deleting position The size of is less than the total number of data .
if (getItemCount() > 0 && position < getItemCount()) {
this.dataList.remove(position);
// notifyItemChanged(position);
notifyItemRemoved(position);
// todo After the delete operation, be sure to call... At the end notifyItemRangeChanged(int positionStart, int itemCount)
// Method refresh to delete the Item Subsequent data , Otherwise, an index exception will occur
notifyItemRangeChanged(position, getItemCount());
}
}
About List Collection delete elements , I saw a better way on the Internet , Just use Iterator Iterator to delete .
if (getItemCount()>0&&position<getItemCount()){
Iterator<T> it = this.dataList.iterator();
while (it.hasNext()) {
if (it.next() == this.dataList.get(position)) {
it.remove();
notifyItemRemoved(position);
// todo After the delete operation, be sure to call... At the end notifyItemRangeChanged(int positionStart, int itemCount)
// Method refresh to delete the Item Subsequent data , Otherwise, an index exception will occur
notifyItemRangeChanged(position, getItemCount());
return;// End the current method
}
}
}
use List.remove() It can lead to List Change in length of , If at this time still carry on to List Errors will be reported during some length related operations , So it's best to use List Determine whether to delete or otherwise , use Iterator.remove() To delete . However, pointer exceptions still occur , So it's better to add another layer of judgment .
边栏推荐
- Official STM32 chip package download address stm32f10x stm32f40x Download
- CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION
- 367. effective complete square dichotomy
- Implementation of multi key state machine based on STM32 standard library
- Mysql配置主从数据库
- A comparison of methods for fully automatic segmentation of tumors and involved nodes in PET/CT of h
- Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning
- 数据库常见面试题(附答案)
- A 3D Dual Path U-Net of Cancer Segmentation Based on MRI
- 数据可视化:数据可视化的意义
猜你喜欢

Closed training (25) basic web security

Student增删gaih

完美二叉树、完全二叉树、完满二叉树

Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit

Data visualization: the four quadrants of data visualization teach you to correctly apply icons

Mysql配置主从数据库

A 3D Dual Path U-Net of Cancer Segmentation Based on MRI

UE4 compile a single file (VS and editor start respectively)

float 与 int 相乘产生的令人崩溃的“ 2.3 * 10 = 22 ”

Kicad learning notes - shortcut keys
随机推荐
安装Anaconda后启动JupyterLab需要输入密码
User level threads and kernel level threads
【NOI模拟赛】为NOI加点料(重链剖分,线段树)
IDEA自动补全
2020-09-21 referer字符串切分 boost gateway代码组织层次
Matlab tips (21) matrix analysis -- partial least squares regression
指针函数和函数指针
FreeRTOS(九)——队列
Mysql配置主从数据库
自定义mvc框架实现
Segmentation of Head and Neck Tumours Using Modified U-net
leetcode MYSQL数据库题目176
券商经理给的开户二维码办理股票开户安全吗?我想开个户
A 2.5D Cancer Segmentation for MRI Images Based on U-Net
UE4 animation redirection
Five heart charity matchmaker team
Automatic Multi-Organ SegmVentation on Abdominal CT With Dense V-Networks
XML布局中Button总是在最上层显示
阿里云服务器安装配置redis,无法远程访问
Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images