问题
Here i am implementing ,
- user pick items
- add in adapter
- adapter current position directly upload to firebase without any click listener
For this code is, In Activity.
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && data != null)
{
switch (requestCode)
{
case PICK_IMAGE:
ImageUri = data.getData();
Log.i("wneuiewnewu",ImageUri.getPath());
UriListFromGallery.add(ImageUri);
postImageAdapter.notifyItemInserted(UriListFromGallery.size()-1);
break;
}
}
}
In Adapter class @Override
public void onBindViewHolder(@NonNull final ImageViewHolder imageViewHolder, final int i) {
final StorageReference filepath=PostReference.child("image").child(currentUserID).child(currentUserID +String.valueOf(tsLong)+FORMAT_IMAGE);
final UploadTask task=filepath.putFile(imagelist.get(i));
task.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
iImageViewHolder.progressBar.setProgress((int) progress);
imageViewHolder.deleteImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
task.cancel();
imagelist.remove(i);
notifyitemremoved(i);
}
});
}
});
}
working is well, Let the problem explain by example, Suppose imagelist has 5 item.
- 0 1 2 3 4 and if 3rd element (index 2) removed..notifyitemremoved(2) is called,so the old element (index 3 & index 4) is now become a (index 2 & index 3). By changing this index onBindViewHolder swap new layout and start one more uploading task. so how to prevent this one more uploading and keep continuing old uploading task ?
来源:https://stackoverflow.com/questions/61452776/how-to-maintain-uploading-task-in-adapter-when-notifyitemremoved-or-notifydatase