在学习用RecyclerView实现ExpandableListView时候,读drawthink/ExpandableRecyclerView
发现如下的代码:
/**
* collapseGroup
* @param position showingDatas position
*/
private int collapseGroup(int position) {
Object item = showingDatas.get(position);
if (null == item) {
return -1;
}
if (!(item instanceof GroupItem)) {
return -1;
}
if (!((GroupItem) item).isExpand()) {
return -1;
}
int tempSize = showingDatas.size();
List<BaseItem> tempChilds;
if (((GroupItem) item).hasChilds()) {
tempChilds = ((GroupItem) item).getChildDatas();
((GroupItem) item).onExpand();
showingDatas.removeAll(tempChilds);
notifyItemRangeRemoved(position+1,tempChilds.size());
notifyItemRangeChanged(position+1,tempSize-(position+1));
return position;
}
return -1;
}
其中notifyItemRangeRemoved() 和 notifyItemRangeChanged()不知如何区别
参考:
关于Adapter调用notifyItemRemoved后数组越界和数据错乱的问题
RecyclerView里notifyItemRemoved的坑
1: 重现crash
- 构造十条数据:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rc_view);
List<String> datas = new ArrayList<>();
for (int i = 0; i < 10; i++) {
datas.add(String.format("第%s个数据", i));
}
RcAdapter rcAdapter = new RcAdapter(this, datas);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(rcAdapter);
}
}
- Adapter:
public class RcAdapter extends RecyclerView.Adapter<RcAdapter.RcViewHolder> {
private List<String> datas;
private Context mContext;
public RcAdapter(Context mContext, List<String> datas) {
this.mContext = mContext;
this.datas = datas;
}
@NonNull
@Override
public RcViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext).inflate(R.layout.rc_item, parent, false);
return new RcViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull RcViewHolder holder, final int position) {
holder.rcItemTv.setText(datas.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("RcAdapter", "点击 position -> " + position);
datas.remove(position);
notifyItemRemoved(position);
}
});
}
@Override
public int getItemCount() {
return datas.size();
}
class RcViewHolder extends RecyclerView.ViewHolder {
TextView rcItemTv;
public RcViewHolder(View itemView) {
super(itemView);
rcItemTv = itemView.findViewById(R.id.textView);
}
}
}
xml rc_item
很简单,就不贴了
共有10条数据,position 0 -> 9
datas.remove(position);
notifyItemRemoved(position);
点击item时候,先移除List里的数据,然后再notifyItemRemoved(pos)
比如我们先点击"第1个数据",正常删除了,没有异常,
此时我们再点击"第9个数据",尝试删除最后一条,就crash了
notify_del_crash.png继续试验,发现此时position已经错乱了,比如我们点击第二个item 删除掉的可能是 第三个item
究其原因:
当我们 datas.remove(position)
数据集刷新了,但是界面上Item所持有的数据没有刷新,比如: 现在的第二个ItemView 是之前的第三个ItemView,我们点击第二个itemView 触发onBindViewHolder(,,position),这个position = 3 ,然后我们又datas.remove(3)
,就是这个造成了点击第二个,移除了第三个的原因,如果不凑巧,我们点击的ItemView的position 超过了当前datas的size,那么自然就IndexOutOfRange了
2: 解决办法
- 2.1 notifyDataSetChanged
这个,干净利索快,不留后遗症,相当于对适配器重新绑定一次数据,啥问题不会再有,就是如果数据过多,会损耗性能能
@Override
public void onBindViewHolder(@NonNull RcViewHolder holder, final int position) {
holder.rcItemTv.setText(datas.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("RcAdapter", "点击 position -> " + position);
datas.remove(position);
notifyDataSetChanged();
}
});
}
但是,随之而来的,删除Item的动画没了
- 2.2 notifyItemRangeChanged
@Override
public void onBindViewHolder(@NonNull RcViewHolder holder, final int position) {
holder.rcItemTv.setText(datas.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("RcAdapter", "点击 position -> " + position);
datas.remove(position);
notifyItemRemoved(position);
/*
* @param positionStart Position of the first item that has changed
* position : 第一个 改变的 item的position ( 旧的 position)
* @param itemCount Number of items that have changed
* itemCount : 已经变更的 item 的数量 (包括自己,因此: datas.size() - position)
*
*/
notifyItemRangeChanged(position, datas.size() - position);
}
});
}
说明一下notifyItemRangeChanged(int positionStart, int itemCount)
的两个参数
/**
* Notify any registered observers that the <code>itemCount</code> items starting at
* position <code>positionStart</code> have changed.
* Equivalent to calling <code>notifyItemRangeChanged(position, itemCount, null);</code>.
*
* <p>This is an item change event, not a structural change event. It indicates that
* any reflection of the data in the given position range is out of date and should
* be updated. The items in the given range retain the same identity.</p>
*
* @param positionStart Position of the first item that has changed
* @param itemCount Number of items that have changed
*
* @see #notifyItemChanged(int)
*/
这个方法,是通知所有观察者: 从positinStart开始的itemCount这些个item已经改变了,与notifyItemRangeChanged(position, itemCount, null)
等价
positionStart : 是从哪个界面位置开始Item开始变化,比如你点击界面上的第二个ItemView positionStart是1
itemCount : 是以经发生变化的item的个数(包括自己,即正在点击这个),比如,你点击界面上的第二个ItemView,position [1,9] 发生变化,共计9个,因此我们上边计算是`datas.size() - position
再返回文章开头
notifyItemRangeRemoved(position+1,tempChilds.size());
notifyItemRangeChanged(position+1,tempSize-(position+1));
原作者批量移除数据后,调用notifyItemRangeChanged(),是统样道理,这里要再解释为什么原作者 position + 1
,因为原作者是要实现ExpandableListView的功能,点击 position,要移除这个position之后的若干个数据,因此是开始移除的位置是 position + 1
好了,本篇文章到此告一段落
祝生活愉快 ヾ(@▽@)ノ
网友评论