【发布时间】:2020-04-01 19:30:20
【问题描述】:
我有一个带有滑动功能的 RecyclerView 以显示删除和编辑按钮。
我添加了:adapter.notifyItemRemoved(position),这个:
adapter.notifyItemRangeChanged(0, adapter.getItemCount());
当点击显示的删除按钮时,删除项目的动画会起作用,并且 该项目已从我的数据库中删除
然后删除的项目重新出现在我的回收站视图中。当我更改活动并返回到具有 recyclerview 的活动时,我应该看到的列表很好。
如果我删除“notifyItemRangeChanged”代码,列表会更新,最后一项重复。
我认为这是我的适配器的 getItemCount 没有正确更新。所以我尝试不同的是首先调用生成列表的方法。这成功了,但是我的删除项目动画现在消失了,因为我猜它只是跳过重新生成列表....
有什么想法吗?
非常感谢您的反馈!
****************** 更新 - 添加适配器类代码 **************** 公共类 RVCategoryAdapter 扩展 RecyclerView.Adapter { 上下文上下文; 列出 categoryItemList;
public RVCategoryAdapter(Context context, List<CategoryItem> categoryItemList) {
this.context = context;
this.categoryItemList = categoryItemList;
}
@NonNull
@Override
public CategoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(R.layout.category_item_layout, parent, false);
return new CategoryViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull CategoryViewHolder holder, final int position) {
final int categoryID;
final String categoryTitle;
Glide.with(context).load(categoryItemList.get(position).getImage()).into(holder.ivCategoryIcon);
holder.txtCatID.setText(""+categoryItemList.get(position).getCategoryID());
holder.txtCategoryTitle.setText(categoryItemList.get(position).getTitle());
holder.txtCategoryDesc.setText(categoryItemList.get(position).getDescription());
categoryID = Integer.parseInt(holder.txtCatID.getText().toString());
categoryTitle = holder.txtCategoryTitle.getText().toString();
holder.cardViewItemLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, NotesListActivity.class);
intent.putExtra("CategoryID", categoryID);
intent.putExtra("CategoryTitle", categoryTitle);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return categoryItemList.size();
}
}
【问题讨论】:
-
你应该粘贴你的适配器类
-
我编辑了我的帖子...
-
显然你没有更新你的内部 ArrayList 所以 getItemCount 将总是返回相同的东西。当您想从 RecyclerView 中删除一个项目时,您必须将其从源列表中删除。
-
@gyosida 这就是我所缺少的!谢谢
标签: android android-recyclerview