Skip to content

Commit

Permalink
may have fixed the recycler view problem where the view would sometim…
Browse files Browse the repository at this point in the history
…es duplicate the images
  • Loading branch information
martinseal committed Feb 23, 2020
1 parent a3ce234 commit 91e4241
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class MultiImageView @JvmOverloads constructor (context: Context, attrs: Attribu
* Add image to view
*/
fun addAllImages(bitmaps: MutableList<Bitmap>) {
this.bitmaps.clear()
this.bitmaps.addAll(bitmaps)
refresh()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import com.stfalcon.multiimageview.sample.helpers.ClickHelper;
import com.stfalcon.multiimageview.sample.models.MultiImageViewModel;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class MultiImageViewAdapter extends RecyclerView.Adapter<MyMultiImageViewHolder> {

Expand Down Expand Up @@ -48,21 +50,52 @@ public MyMultiImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
public void onBindViewHolder(@NonNull final MyMultiImageViewHolder holder, int position) {
MultiImageViewModel model = multiImageViewModels.get(position);
holder.multiImageView.clear();
holder.imageSetCounter.set(0);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
holder.multiImageView.setTransitionName("transitionName-" + model.getId());
}
for (Integer s : model.getImages()){
setImages(holder, s);

if (!model.getImages().isEmpty()){
// setImages(holder, model.getImages(), holder.imageSetCounter);
getAndSetImages(holder,model.getImages(), holder.imageSetCounter, new ArrayList<Bitmap>());
}

}

private void getAndSetImages(final MyMultiImageViewHolder holder, final List<Integer> imageIdentifiers, final AtomicInteger imageSetCount, final List<Bitmap> bitmaps){

glide.asBitmap().load(imageIdentifiers.get(imageSetCount.get())).into(new CustomTarget<Bitmap>() {

@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable com.bumptech.glide.request.transition.Transition<? super Bitmap> transition) {
bitmaps.add(resource);
if (imageSetCount.get() < imageIdentifiers.size() - 1){
imageSetCount.getAndIncrement();
getAndSetImages(holder,imageIdentifiers, imageSetCount, bitmaps);
} else {
holder.multiImageView.addAllImages(bitmaps);
}
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {

}
});
}

private void setImages(@NonNull final MyMultiImageViewHolder holder, Integer s) {
private void setImages(@NonNull final MyMultiImageViewHolder holder, final List<Integer> imageIdentifiers, final AtomicInteger imageSetCount) {

glide.asBitmap().load(s).into(new CustomTarget<Bitmap>() {
glide.asBitmap().load(imageIdentifiers.get(imageSetCount.get())).into(new CustomTarget<Bitmap>() {

@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable com.bumptech.glide.request.transition.Transition<? super Bitmap> transition) {
holder.multiImageView.addImage(resource);
if (imageSetCount.get() < imageIdentifiers.size() - 1){
imageSetCount.getAndIncrement();
setImages(holder, imageIdentifiers, imageSetCount);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import com.stfalcon.multiimageview.sample.R;
import com.stfalcon.multiimageview.sample.helpers.ClickHelper;

import java.util.concurrent.atomic.AtomicInteger;

public class MyMultiImageViewHolder extends RecyclerView.ViewHolder {

public MultiImageView multiImageView;
public AtomicInteger imageSetCounter = new AtomicInteger(0);

public MyMultiImageViewHolder(@NonNull View itemView, final ClickHelper clickHelper) {
super(itemView);
Expand Down

0 comments on commit 91e4241

Please sign in to comment.