我已经实现了片段中的循环器视图,只需点击一个项目我显示一个对话框,然后通过选择删除项目我正在删除它我面临的问题是通过调用notifyItemRemoved()它不是应该回收的回收者 但是我在完成一些后台工作后在一个线程中调用notifyItemRemoved()请指导我做错了什么 这是我的 适配器类
public class SavedAlbumAdapter2 extends RecyclerView.Adapter<SavedAlbumAdapter2.ItemViewHolder> {
private final GlideRequest<Drawable> request;
private ArrayList<AlbumLocalObj> itemModels;
private static final int LIST_ITEM = 0;
private static final int GRID_ITEM = 1;
private ClickListener mListener;
private LocalPreferences localPreferences;
public SavedAlbumAdapter2(Activity context, ArrayList<AlbumLocalObj> values, ClickListener clickListener) {
itemModels = values;
this.mListener = clickListener;
localPreferences = new LocalPreferences(context);
int reqWidth = ((HomeActivity) context).screenWidth / 2;
request = GlideApp.with(context)
.asDrawable()
.centerCrop()
.override(reqWidth, reqWidth)
.transition(withNoTransition());
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
if (viewType == LIST_ITEM) {
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_gallery2, parent, false);
} else {
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_gallery, parent, false);
}
return new ItemViewHolder(itemView, mListener);
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
AlbumLocalObj model = itemModels.get(position);
holder.img_label.setText(model.getName());
holder.tv_count.setText(String.valueOf(model.getCount()));
initializeViews(model, holder, position);
}
@Override
public int getItemViewType(int position) {
if (localPreferences.isGridViewEnabled()) {
return GRID_ITEM;
} else {
return LIST_ITEM;
}
}
private void initializeViews(AlbumLocalObj model, final ItemViewHolder holder, int position) {
if (model.getMediaFileObject() == null)
holder.gallery_img.setImageResource(R.drawable.ic_empty_fol);
else {
request.load(model.getMediaFileObject()).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
Log.i("glide_action", "failure");
clearResource((MediaFileObject) model);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
clearResource((MediaFileObject) model);
Log.i("glide_action", "success");
return false;
}
})
.into(holder.gallery_img);
}
}
public interface ClickListener {
void onItemClick(View view, int piecesAtPos);
void onItemLongClick(View view, int piecesAtPos);
}
private void clearResource(MediaFileObject item) {
try {
if (item.getInputStream() != null) {
Log.i("resource", "clear");
item.getInputStream().close();
item.setInputStream(null);
//Log.i("closing_str", pos + "");
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return itemModels.size();
}
public static class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
SquarImageView gallery_img;
TextView img_label;
TextView tv_count;
ImageButton info;
private ClickListener mListener;
ItemViewHolder(View itemView, ClickListener listener) {
super(itemView);
gallery_img = itemView.findViewById(R.id.img);
info = itemView.findViewById(R.id.info);
img_label = itemView.findViewById(R.id.label);
tv_count = itemView.findViewById(R.id.count);
this.mListener = listener;
info.setOnClickListener(this);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
@Override
public void onClick(View view) {
mListener.onItemClick(view, getAdapterPosition());
}
@Override
public boolean onLongClick(View view) {
mListener.onItemLongClick(view, getAdapterPosition());
return false;
}
}
}
这是我的片段方法,我从中删除一个项目(相册)我在onItemClick上调用此方法(对话框选项(删除))
private void deleteAlbum(final int selectedAlbum) {
if (list.get(selectedAlbum).getCount() == 0) {
//return true if recycle bin contain some files check db function
if (localDatabase.deleteAlbum(list.get(selectedAlbum).getId()))
showRecycleBinContainSomeFiles();
updateAlbumRecord();
return;
}
Runnable runnable = new Runnable() {
@Override
public void run() {
int progress = 0;
ArrayList<MediaFileObject> selection = localDatabase.getAllMediaObjectsOfID(list.get(selectedAlbum).getId());
for (MediaFileObject obj : selection) {
if (searializeableTasks.isCancelled())
break;
localDatabase.deleteMediaObject(obj);
progress++;
if (searializeableTasks.isCancelled())
sendProgressToHandle(-101);
else
sendProgressToHandle(progress);
}
if (!searializeableTasks.isCancelled()) {
localDatabase.deleteAlbum(list.get(selectedAlbum).getId());
list.remove(selectedAlbum);
Log.e("selectedAlbum", selectedAlbum + "");
}
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
savedAlbumAdapter.notifyItemRemoved(selectedAlbum);
savedAlbumAdapter.notifyItemRangeChanged(selectedAlbum, list.size());
Log.e("sizes",list.size()+""+savedAlbumAdapter.getItemCount());
///savedAlbumAdapter.notifyDataSetChanged();
//updateAlbumRecord();
}
});
}
};
searializeableTasks.startTasks(localDatabase.getAllMediaObjectsOfID(list.get(selectedAlbum).getId()).size(), runnable);
}
并且如果我在list.remove(selectedAlbum);
之前的处理程序帖子中放置此行savedAlbumAdapter.notifyItemRemoved(selectedAlbum);
它将删除下一个项目,当我重新启动应用程序时,下一个被删除的项目回来了它非常奇怪的请帮助我做错了
并请指导是否可以进行任何改进谢谢:)