From 91e42413cf391cae9a7c6c9909ccccf2bd7c39d5 Mon Sep 17 00:00:00 2001 From: martin seal Date: Sun, 23 Feb 2020 08:37:49 +0000 Subject: [PATCH] may have fixed the recycler view problem where the view would sometimes duplicate the images --- .../stfalcon/multiimageview/MultiImageView.kt | 1 + .../adapters/MultiImageViewAdapter.java | 41 +++++++++++++++++-- .../view_holders/MyMultiImageViewHolder.java | 3 ++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/multiimageview/src/main/java/com/stfalcon/multiimageview/MultiImageView.kt b/multiimageview/src/main/java/com/stfalcon/multiimageview/MultiImageView.kt index 0b4dbed..1b66850 100644 --- a/multiimageview/src/main/java/com/stfalcon/multiimageview/MultiImageView.kt +++ b/multiimageview/src/main/java/com/stfalcon/multiimageview/MultiImageView.kt @@ -65,6 +65,7 @@ class MultiImageView @JvmOverloads constructor (context: Context, attrs: Attribu * Add image to view */ fun addAllImages(bitmaps: MutableList) { + this.bitmaps.clear() this.bitmaps.addAll(bitmaps) refresh() } diff --git a/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/MultiImageViewAdapter.java b/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/MultiImageViewAdapter.java index be75206..d5cc920 100644 --- a/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/MultiImageViewAdapter.java +++ b/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/MultiImageViewAdapter.java @@ -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 { @@ -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()); } + + } + + private void getAndSetImages(final MyMultiImageViewHolder holder, final List imageIdentifiers, final AtomicInteger imageSetCount, final List bitmaps){ + + glide.asBitmap().load(imageIdentifiers.get(imageSetCount.get())).into(new CustomTarget() { + + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable com.bumptech.glide.request.transition.Transition 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 imageIdentifiers, final AtomicInteger imageSetCount) { - glide.asBitmap().load(s).into(new CustomTarget() { + glide.asBitmap().load(imageIdentifiers.get(imageSetCount.get())).into(new CustomTarget() { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable com.bumptech.glide.request.transition.Transition transition) { holder.multiImageView.addImage(resource); + if (imageSetCount.get() < imageIdentifiers.size() - 1){ + imageSetCount.getAndIncrement(); + setImages(holder, imageIdentifiers, imageSetCount); + } } @Override diff --git a/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/view_holders/MyMultiImageViewHolder.java b/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/view_holders/MyMultiImageViewHolder.java index c92681b..62f99bc 100644 --- a/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/view_holders/MyMultiImageViewHolder.java +++ b/sample/src/main/java/com/stfalcon/multiimageview/sample/adapters/view_holders/MyMultiImageViewHolder.java @@ -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);