我正在创建一个卡片列表,使用RecyclerView进行显示。每个卡片都有一个按钮,用于将该卡片从列表中删除。
但当我使用 notifyItemRemoved() 来删除 RecyclerView 中的卡片时,它会移除该项并且动画效果良好,但是列表中的数据没有被正确更新。
相反,如果我改用 notifyDataSetChanged() ,那么列表中的项目将被正确地移除和更新,但是卡片不会出现动画效果。
有没有人在使用 notifyItemRemoved() 时有任何经验,并知道它为什么与 notifyDataSetChanged 表现不同?
这里是我正在使用的一些代码:
private List<DetectedIssue> issues = new ArrayList<DetectedIssue>();
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
if(position >0){
RiskViewHolder riskHolder = (RiskViewHolder)holder;
final int index = position - 1;
final DetectedIssue anIssue = issues.get(index);
riskHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
int index = issues.indexOf(anIssue);
issues.remove(anIssue);
notifyItemRemoved(index);
//notifyDataSetChanged();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
}
@Override
public int getItemCount() {
return (issues.size()+1);
}