java - Prevent RecyclerView showing previous content when scrolling -
i have recyclerview gridlinearlayout , custom adapter. content of each item picture downloaded using json , parsing it.
basically grid of pictures.
everything works fine, however, when scrolling down content, , going again, shows previous views in each item less second, , show proper picture again.
what prevent or fix this? in advance and/or guidance provide.
this adapter code:
package jahirfiquitiva.project.adapters; import android.content.context; import android.graphics.bitmap; import android.support.v7.graphics.palette; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.view.animation.animation; import android.view.animation.animationutils; import android.widget.imageview; import android.widget.linearlayout; import android.widget.progressbar; import android.widget.textview; import jahirfiquitiva.project.activities.wallpapersactivity; import com.koushikdutta.async.future.futurecallback; import com.koushikdutta.ion.ion; import java.util.arraylist; import java.util.hashmap; import java.util.map; import java.util.weakhashmap; import jahirfiquitiva.project.r; public class wallpapersadapter extends recyclerview.adapter<wallpapersadapter.wallsholder> { public interface clicklistener { void onclick(wallsholder view, int index, boolean longclick); } private arraylist<hashmap<string, string>> data; private final context context; private boolean usepalette = true; private final clicklistener mcallback; private final map<string, palette> mpalettecache = new weakhashmap<>(); public wallpapersadapter(context context, clicklistener callback) { this.context = context; this.data = new arraylist<>(); this.mcallback = callback; } public void setdata(arraylist<hashmap<string, string>> data) { this.data = data; notifydatasetchanged(); } @override public wallsholder oncreateviewholder(viewgroup parent, int viewtype) { layoutinflater inflater = layoutinflater.from(context); return new wallsholder(inflater.inflate(r.layout.wallpaper_item, parent, false)); } @override public void onbindviewholder(final wallsholder holder, int position) { animation anim = animationutils.loadanimation(context, android.r.anim.fade_in); hashmap<string, string> jsondata = data.get(position); holder.name.settext(jsondata.get(wallpapersactivity.name)); final string wallurl = jsondata.get(wallpapersactivity.wall); holder.wall.startanimation(anim); holder.wall.settag(wallurl); ion.with(context) .load(wallurl) .asbitmap() .setcallback(new futurecallback<bitmap>() { @override public void oncompleted(exception e, bitmap result) { holder.progressbar.setvisibility(view.gone); if (e != null) { e.printstacktrace(); } else if (holder.wall.gettag() != null && holder.wall.gettag().equals(wallurl)) { holder.wall.setimagebitmap(result); if (usepalette) { palette p; if (mpalettecache.containskey(wallurl)) { p = mpalettecache.get(wallurl); } else { p = new palette.builder(result).generate(); mpalettecache.put(wallurl, p); } if (p != null) { palette.swatch wallswatch = p.getvibrantswatch(); if (wallswatch != null) { holder.titlebg.setbackgroundcolor(wallswatch.getrgb()); holder.titlebg.setalpha(1); holder.name.settextcolor(wallswatch.gettitletextcolor()); holder.name.setalpha(1); } } } } } }); } @override public int getitemcount() { return data.size(); } public class wallsholder extends recyclerview.viewholder implements view.onclicklistener, view.onlongclicklistener { public final view view; public final imageview wall; public final textview name; public final progressbar progressbar; public final linearlayout titlebg; wallsholder(view v) { super(v); view = v; wall = (imageview) v.findviewbyid(r.id.wall); name = (textview) v.findviewbyid(r.id.name); progressbar = (progressbar) v.findviewbyid(r.id.progress); titlebg = (linearlayout) v.findviewbyid(r.id.titlebg); view.setonclicklistener(this); view.setonlongclicklistener(this); } @override public void onclick(view v) { int index = getlayoutposition(); if (mcallback != null) mcallback.onclick(this, index, false); } @override public boolean onlongclick(view v) { int index = getlayoutposition(); if (mcallback != null) mcallback.onclick(this, index, true); return false; } } }
as name suggests recyclerview
recycles views optimize memory displays content of previous view. since loading image internet takes little time load image content of previous image observed. can 1 of following things.
1) set default image local resource before loading actual image, preferably small size image preserve memory.something this
//load default image first holder.wall.setimageresource(r.id.your_default_image_resource); //load actual image ion.with(context) .load(wallurl) .asbitmap() .setcallback(new futurecallback<bitmap>() { @override public void oncompleted(exception e, bitmap result) { holder.progressbar.setvisibility(view.gone); if (e != null) { e.printstacktrace(); } else if (holder.wall.gettag() != null && holder.wall.gettag().equals(wallurl)) { holder.wall.setimagebitmap(result); if (usepalette) { palette p; if (mpalettecache.containskey(wallurl)) { p = mpalettecache.get(wallurl); } else { p = new palette.builder(result).generate(); mpalettecache.put(wallurl, p); } if (p != null) { palette.swatch wallswatch = p.getvibrantswatch(); if (wallswatch != null) { holder.titlebg.setbackgroundcolor(wallswatch.getrgb()); holder.titlebg.setalpha(1); holder.name.settextcolor(wallswatch.gettitletextcolor()); holder.name.setalpha(1); } } } } } });
2) set imageview
visibility gone/invisible before loading image , make visible again after image loaded.
//hide imageview holder.wall.setvisibility(view.invisible); ion.with(context) .load(wallurl) .asbitmap() .setcallback(new futurecallback<bitmap>() { @override public void oncompleted(exception e, bitmap result) { holder.progressbar.setvisibility(view.gone); if (e != null) { e.printstacktrace(); } else if (holder.wall.gettag() != null && holder.wall.gettag().equals(wallurl)) { //show imageview , set bitmap holder.wall.setvisibility(view.visible); holder.wall.setimagebitmap(result); if (usepalette) { palette p; if (mpalettecache.containskey(wallurl)) { p = mpalettecache.get(wallurl); } else { p = new palette.builder(result).generate(); mpalettecache.put(wallurl, p); } if (p != null) { palette.swatch wallswatch = p.getvibrantswatch(); if (wallswatch != null) { holder.titlebg.setbackgroundcolor(wallswatch.getrgb()); holder.titlebg.setalpha(1); holder.name.settextcolor(wallswatch.gettitletextcolor()); holder.name.setalpha(1); } } } } } });
Comments
Post a Comment