我有一个图库,其中列出了我编辑过的图像。图片列表来自我的活动。每当我添加一个项目时,一切都很好。
例如,我有八个项目。我想删除其中的六个。没问题;一切正常。
但是现在,我要一个一个地删除其他两个项目。当我单击其中一个的删除底部时,两个都被删除。我该如何解决这个问题?
以下是我的适配器类和活动:
public class EditedImageListAdapter extends RecyclerView.Adapter<EdittedListViewHolder> {
private Context context;
private ArrayList<File> mFiles;
public EditedImageListAdapter(Context context) {
this.context = context;
refresh();
}
public void refresh() {
String imagesFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();
File downloadedImagesDir = new File(imagesFolder, "Selfie Editor");
File[] files = downloadedImagesDir.listFiles();
mFiles = files == null
? new ArrayList<File>()
: new ArrayList<File>(Arrays.asList(files));
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public EdittedListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_editted_list, parent, false);
return new EdittedListViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull EdittedListViewHolder holder, final int position) {
Log.d("Files", "FileName:" + mFiles.get(position).getName());
Glide.with(context).load(mFiles.get(position)).into(holder.listItem);
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View v, int position, boolean isLongClick) {
// show image
Intent showImageIntent = new Intent(context, ResultActivity.class);
showImageIntent.putExtra(Consts.SHOW_IMAGE, mFiles.get(position).getAbsolutePath());
context.startActivity(showImageIntent);
}
});
holder.shareImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// share image
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(mFiles.get(position).getPath()));
context.startActivity(Intent.createChooser(sharingIntent, "Share"));
}
});
holder.deleteImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//delete image
//todo when we have two items deleting both of them (except for this problem working well)
mFiles.get(position).delete();
EditedImageListAdapter.this.mFiles.remove(position);
EditedImageListAdapter.this.notifyItemRemoved(position);
EditedImageListAdapter.this.notifyItemRangeRemoved(position, getItemCount() - position);
EditedImageListAdapter.this.notifyItemRemoved(position);
}
});
}
@Override
public int getItemCount() {
return mFiles.size();
}
}
public class EdittedList extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
RecyclerView recycler_list;
EditedImageListAdapter adapter;
private final int REQUEST_IMAGE_CAPTURE = 112;
private final int ADOBE_FROM_GALLERY_REQUEST_CODE = 2;
private final int ADOBE_FROM_CAPTURE_REQUEST_CODE = 3;
private final int RESULT_GALLERY_IMAGE = 123;
public static final int WRITE_EXTERNAL_STORAGE = 6;
protected Uri mGalleryImageUri;
protected Uri mCapturedImageUri;
Uri editedImageUri;
String fontFamily_editedList = "MarkPro-Bold.otf";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editted_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(getResources().getColor(R.color.colorblack));
toolbar.setTitle("Edited List");
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
changeNavigationMenuItemTypeFace(navigationView);
//recyclerview initialize
recycler_list = findViewById(R.id.recycler_list);
RecyclerView.LayoutManager layoutManager = new SpannableGridLayoutManager(new SpannableGridLayoutManager.GridSpanLookup() {
@Override
public SpannableGridLayoutManager.SpanInfo getSpanInfo(int position) {
if (position == 0) {
return new SpannableGridLayoutManager.SpanInfo(2, 2);
} else {
return new SpannableGridLayoutManager.SpanInfo(1, 1);
}
}
}, 3, 0.8f);
recycler_list.setLayoutManager(layoutManager);
adapter = new EditedImageListAdapter(this);
adapter.notifyDataSetChanged();
recycler_list.setAdapter(adapter);
recycler_list.setHasFixedSize(true);
new Calligrapher(this).setFont(this, fontFamily_editedList, true);
}
@Override
protected void onResume() {
super.onResume();
adapter.refresh();
adapter.notifyDataSetChanged();
}
答案 0 :(得分:0)
您要重复以下功能:
mFiles.get(position).delete();
EditedImageListAdapter.this.mFiles.remove(position);
例如,有2个文件A,B。mFile.get(0)= A,mFile.get(1)= B,
执行时:
mFiles.get(position).delete();
文件B倒退,例如mFile.get(0)= B;并且A被删除,
并与此:
EditedImageListAdapter.this.mFiles.remove(position);
它也删除了第二个。