bumptech / glide

An image loading and caching library for Android focused on smooth scrolling
https://bumptech.github.io/glide/
Other
34.59k stars 6.12k forks source link

Question about SimpleTarget for our case #3294

Closed paulocoutinhox closed 6 years ago

paulocoutinhox commented 6 years ago

Glide Version: 4.8 Integration libraries: okhttp Device/Android Version: S8

Issue details / Repro steps / Use case background:

Before update to latest version we use glide in this way for our product carousels:

public class ProductCarouselAdapter extends RecyclerView.Adapter<ProductCarouselAdapter.ViewHolder> {

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        Product product = productList.get(position);

        if (holder.image != null) {
            holder.target = new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, Transition transition) {
                    holder.image.setImageBitmap(resource);
                    holder.image.setVisibility(View.VISIBLE);

                    if (holder.image != null) {
                        holder.imageProgress.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onLoadFailed(@Nullable Drawable errorDrawable) {
                    holder.image.setImageDrawable(errorDrawable);
                    holder.image.setVisibility(View.VISIBLE);

                    if (holder.imageProgress != null) {
                        holder.imageProgress.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onLoadStarted(@Nullable Drawable placeholder) {
                    holder.image.setImageDrawable(placeholder);
                    holder.image.setVisibility(View.GONE);

                    if (holder.imageProgress != null) {
                        holder.imageProgress.setVisibility(View.VISIBLE);
                    }
                }
            };

            GlideApp.with(context)
                    .asBitmap()
                    .placeholder(UIUtil.drawableColorChange(context, R.drawable.ic_product_placeholder, R.color.grey_800))
                    .load(AppUtil.getImageUrlFromNameAndSize(product.getCoverImage(), product.getCatalogType(), (int) context.getResources().getDimension(R.dimen.cover_image_width), (int) context.getResources().getDimension(R.dimen.cover_image_height)))
                    .error(UIUtil.drawableColorChange(context, R.drawable.ic_product_placeholder, R.color.grey_800))
                    .into(holder.target);
        }

        if (holder.title != null && holder.subTitle != null) {
            holder.title.setText(product.getTitle());
            holder.subTitle.setText(product.getSubtitle());
        }
    }

    @Override
    public void onViewRecycled(ViewHolder holder) {
        super.onViewRecycled(holder);

        if (holder.target != null) {
            try {
                GlideApp.with(context).clear(holder.target);
            } catch (Exception e) {
                e.printStackTrace();
            }

            holder.target = null;
        }
    }

}

But after update, SimpleTarget was deprecated. I don't know if have a better way to do what we need.

Can anyone help me?

sjudd commented 6 years ago

Use Target directly and make sure you implement onLoadCleared. Check the javadoc.

stale[bot] commented 6 years ago

This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.