当前位置:网站首页>RecyclerView刷新闪烁与删除Item时崩溃问题
RecyclerView刷新闪烁与删除Item时崩溃问题
2022-06-29 09:13:00 【枫林梦】
RecyclerView刷新闪烁与删除Item时崩溃问题
/**
* todo RecyclerView刷新闪烁 适用于刷新单个多个Item
* 问题原因分析:
* RecyclerView有一个RecyclerViewPool用来缓存已创建的item,不手动创建RecyclerViewPool时,系统会自动创建一个大小为5
* 的RecyclerViewPool。
* 这就意味着RecyclerView的item个数大于5,每次刷新都会自动调用onCreateViewHolder()。
* 新创建的item不配置相关内容的话,将显示布局文件中的默认值。从而导致有数据和无数据的闪烁。
* <p>
* 问题解决方案:
* 手动创建给RecyclerViewPool对象,指定它的大小,再传给RecyclerView对象。
* <p>
* 来源: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刷新闪烁 适用于刷新单个多个Item
* 问题原因分析:
* RecyclerView山下滑动、添加新的数据的时候,每次都会自动调动默认的动画方法,其表现方式为将画面
* 从 0 -> 1 设置一遍,这就会导致整个RecyclerView从无到有显示一遍,由此出现闪烁情况
* <p>
* 问题解决方案:
* 通过设置关闭RecyclerView的动画效果
* <p>
* 来源: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刷新闪烁 适用于刷新单个Item
* 问题原因分析:
* 因为notifyDataSetChanged();刷新的是整个RecyclerView,而我们此时仅需要刷单个Item而已
* <p>
* 问题解决方案:
* 使用notifyItemChanged(int position);刷新指定位置的Item
*
* @param adapter
* @param position
*/
public static void noShank(CommonAdapter adapter, int position) {
adapter.notifyItemChanged(position);
}
**
* 删除指定索引处的数据并刷新Item
*/
public void deleteIndexData(int position) {
//当用户快速点击删除的时候可能会在Item仅剩两个的时候报索引异常,原因是position=1处的Item已经执行删除操作
//但是并没有来的及删除出去,此时数据大小为1,所以会报错。
//解决办法是在执行删除操作的时候判断一下position的大小时候小于数据总数。
if (getItemCount() > 0 && position < getItemCount()) {
this.dataList.remove(position);
// notifyItemChanged(position);
notifyItemRemoved(position);
// todo 删除操作后一定要在最后调用notifyItemRangeChanged(int positionStart, int itemCount)
// 方法刷新删除该Item其后的数据,否则会索引异常
notifyItemRangeChanged(position, getItemCount());
}
}
关于List集合删除元素,在网上看到了更好的办法,就是用Iterator迭代器进行删除操作。
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 删除操作后一定要在最后调用notifyItemRangeChanged(int positionStart, int itemCount)
// 方法刷新删除该Item其后的数据,否则会索引异常
notifyItemRangeChanged(position, getItemCount());
return;//结束当前的方法
}
}
}
用List.remove()会导致List的长度变化,如果在这时候仍旧进行对List长度相关的一些操作的时候就会报错,所以最好是用List进行是否删除或者其他的判断,用Iterator.remove()来进行删除。但还是会出现指针异常,所以最好还是再加一层判断。
边栏推荐
猜你喜欢

Zabbix4.4 configure the indicators of the monitoring server and solve the garbled graphics pages

User level threads and kernel level threads

Custom MVC framework implementation

Segmentation of Head and Neck Tumours Using Modified U-net

In the era of data processing, data quality construction is the way for enterprises to survive

KiCad学习笔记--快捷键

Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images

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

MySQL configuring master-slave databases

C语言中通过sprintf()函数构造sql语句
随机推荐
linux下centos7中mysql5.7安装教程
数据治理:元数据管理(第二篇)
长安链数据存储介绍及Mysql存储环境搭建
Segmentation of Head and Neck Tumours Using Modified U-net
数据可视化:数据可视化的意义
阿里云服务器安装配置redis,无法远程访问
动态规划总结
Set up lamp environment under cenos7
我想知道如何免费网上注册股票开户?另外,手机开户安全么?
监控数据源连接池使用情况
[technology development] development and design of alcohol tester solution
Go deep into RC, RS, daemonset and statefulset (VII)
float 与 int 相乘产生的令人崩溃的“ 2.3 * 10 = 22 ”
Basic operations of MAC MySQL database
Is it safe to open an account for stock speculation? Is it reliable?
Generic paging framework
UE4 material UV texture does not stretch with model scale
Mysql配置主从数据库
MATLAB小技巧(21)矩阵分析--偏最小二乘回归
数据可视化:数据可视化四象限,教你正确应用图标