当前位置:网站首页>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 .
边栏推荐
- Go deep into RC, RS, daemonset and statefulset (VII)
- 数据可视化:数据可视化的意义
- 长安链GO语言智能合约环境搭建及使用
- Official STM32 chip package download address stm32f10x stm32f40x Download
- Wechat applet rewrites the page function to realize global logging
- 微信小程序重写Page函数,实现全局日志记录
- Data governance: Metadata Management (Part 2)
- 367. 有效的完全平方数-二分法
- Cisco ASA、FTD和HyperFlex HX的漏洞分析复现
- 数据治理:元数据管理(第二篇)
猜你喜欢

User level threads and kernel level threads

阿里云服务器安装配置redis,无法远程访问

Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning

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

装饰器模式的应用,包装ServletRequest,增加addParameter方法

Student addition / deletion gaih

基于PyQt5和Qt Designer的简易加法计算器的制作

1424. 对角线遍历 II

Introduction to Chang'an chain data storage and construction of MySQL storage environment

KDevelop new project
随机推荐
c#判断数组是否包含另一个数组的任何项
2020-09-25 boost库的noncopyable,用于单例模式
ORA-01950 对表空间无权限
Matlab tips (21) matrix analysis -- partial least squares regression
请用已学过的知识编写程序,找出小甲鱼藏在下边这个长字符串中的密码,密码的埋藏点符合以下规律:
证券账号开户安全吗?是靠谱的吗?
FreeRTOS(八)——时间管理
Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning
Wechat applet implements the data listener watch, including the watch that destroys the watch and sub attributes
滑块验证代码
Go deep into RC, RS, daemonset and statefulset (VII)
我想要股票开户优惠,怎么得到?还有,在线开户安全么?
A 3D Dual Path U-Net of Cancer Segmentation Based on MRI
Chang'an chain go language smart contract writing and compilation
装饰器模式的应用,包装ServletRequest,增加addParameter方法
leetcode MYSQL数据库题目181
股票炒股账号开户安全吗?是靠谱的吗?
共用体Union
Introduction to Chang'an chain data storage and construction of MySQL storage environment
Making of simple addition calculator based on pyqt5 and QT Designer