我正在尝试在recycleview 中实现“滑动删除”项功能。但是当在snackbar上调用UNDO动作时,被删除的项目无法正确恢复。
这是错误。 https://i.imgur.com/hPfaBQX.png
我试图以onChildDraw
不同的方式实现,但它不起作用
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
// backup of removed item
final String deletedItem = adapter.arrIgnoreNumber.get(viewHolder.getAdapterPosition());
final int deletedIndex = viewHolder.getAdapterPosition();
adapter.removeItem(viewHolder.getAdapterPosition());
// showing snack bar with Undo option
Snackbar snackbar = Snackbar.make(coordinatorLayout, "Removed from the list!", Snackbar.LENGTH_LONG);
snackbar.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {
adapter.restoreItem(deletedItem, deletedIndex);
}
});
snackbar.setActionTextColor(Color.YELLOW);
snackbar.show();
}
}
@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
Bitmap icon;
Paint p = new Paint();
if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE){
View itemView = viewHolder.itemView;
float height = (float) itemView.getBottom() - (float) itemView.getTop();
float width = height / 3;
if(dX > 0){
p.setColor(Color.parseColor("#388E3C"));
RectF background = new RectF((float) itemView.getLeft(), (float) itemView.getTop(), dX,(float) itemView.getBottom());
c.drawRect(background,p);
icon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_delete);
RectF icon_dest = new RectF((float) itemView.getLeft() + width ,(float) itemView.getTop() + width,(float) itemView.getLeft()+ 2*width,(float)itemView.getBottom() - width);
c.drawBitmap(icon,null,icon_dest,p);
} else {
p.setColor(Color.parseColor("#D32F2F"));
RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop(),(float) itemView.getRight(), (float) itemView.getBottom());
c.drawRect(background,p);
icon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_delete);
RectF icon_dest = new RectF((float) itemView.getRight() - 2*width ,(float) itemView.getTop() + width,(float) itemView.getRight() - width,(float)itemView.getBottom() - width);
c.drawBitmap(icon,null,icon_dest,p);
}
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
我希望删除的项目可以在列表中恢复
- 更新:我提供我的适配器以获取更多详细信息:
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.list_ignore_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
final int tmpPos = position;
viewHolder.phone.setText(arrIgnoreNumber.get(position));
String name = arrIgnoreNumber.get(position);
if (!name.isEmpty()) {
viewHolder.phone.setVisibility(View.VISIBLE);
viewHolder.name.setText(name);
} else {
viewHolder.phone.setVisibility(View.GONE);
viewHolder.name.setText(arrIgnoreNumber.get(position));
}
viewHolder.icon.setImageBitmap(Utils.getBitmapByContactNumber(mContext, arrIgnoreNumber.get(position), false));
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return arrIgnoreNumber.size();
}
public void removeItem(int position) {
arrIgnoreNumber.remove(position);
notifyItemRemoved(position);
}
public void restoreItem(String item, int position) {
arrIgnoreNumber.add(position, item);
notifyItemInserted(position);
}