Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recyclerview Adapter and Glide - same image every 4-5 rows

I have this problem - just for testing purposes I added ParseFile to one of ParseObject from received list. Instead of showing it only in that row it shows every 4-5 rows, sometimes more, sometimes less. I supspect that recycling view have something to do with this. Strangely, other data (deleted from this example) works fine with position variable.

@Override     public void onBindViewHolder(ViewHolder holder, int position) {         if(parseList.get(position).get("logo") != null){             ParseFile image = (ParseFile) parseList.get(position).get("logo");             String url = image.getUrl();             Glide.with(context)                     .load(url)                     .placeholder(R.drawable.piwo_48)                     .transform(new CircleTransform(context))                     .into(holder.imageView);           }      } 
like image 416
jean d'arme Avatar asked Sep 21 '15 23:09

jean d'arme


People also ask

How do I use Glide with RecyclerView?

Create a layout for images To create grid layout, we'll use RecyclerView in our app. To create a RecyclerView item layout file, right-click the layout folder and select New, then Layout Resource File. After that, fill in the name of the file. After creating the file, we'll create our item layout using CardView .

Is there an Addheaderview equivalent for RecyclerView?

addItemDecoration(headerDecoration); The decoration is also reusable since there is no need to modify the adapter or the RecyclerView at all.

How many times is called onCreateViewHolder?

By default it have 5. you can increase as per your need. Save this answer.


1 Answers

The answers here are incorrect, although they're on the right track.

You need to call Glide#clear(), not just set the image drawable to null. If you don't call clear(), an async load completing out of order may still cause view recycling issues. Your code should look like this:

@Override  public void onBindViewHolder(ViewHolder holder, int position) {     if (parseList.get(position).get("logo") != null) {         ParseFile image = (ParseFile) parseList.get(position).get("logo");         String url = image.getUrl();         Glide.with(context)                  .load(url)                 .placeholder(R.drawable.piwo_48)                 .transform(new CircleTransform(context))                  .into(holder.imageView);     } else {         // make sure Glide doesn't load anything into this view until told otherwise         Glide.with(context).clear(holder.imageView);         // remove the placeholder (optional); read comments below         holder.imageView.setImageDrawable(null);     } }  
like image 121
Sam Judd Avatar answered Oct 09 '22 04:10

Sam Judd