android - RecyclerView Recycled ViewHolder Image View wrong size -
i have recycler view different view holders.
a couple of view holders have image views pass glide display images.
the problem is, when recycler view starts recycling views, imageview width/height of recycled view display image incorrectly.
here imageview:
<imageview android:id="@+id/image" android:layout_margintop="@dimen/feed_item_margin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"/>
this passed glide
glide.with(itemview.getcontext()) .load(uri.parse(mediautils .getmedia(feedcontent).getmediaurl())) .placeholder(r.drawable.placeholder) .diskcachestrategy(diskcachestrategy.source) .crossfade().into(image);
this works until recyclerview starts recycling first image in recyclerview looks how it's meant look.
however, when scroll away item , scroll looks this:
so image has become distorted , not not full width of parent.
i want image view wrap content because images different heights etc.. test added line holder.setisrecyclable(false);
prevent recycling of particular holder , images displayed should, however, expected gave jarring effect.
so tried resetting params of image view in onviewrecycled
method so:
@override public void onviewrecycled(abstractholder viewholder){ super.onviewrecycled(viewholder); int position = viewholder.getadapterposition(); ifeedcontent content = mfeedcontentlist.get(position); linearlayout.layoutparams params = new linearlayout.layoutparams(viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content); params.setmargins(0, (int) utils.dptopx(mcontext,10),0,0); params.gravity = gravity.center; if(isimage(content)){ viewholder.getimageview().setimageuri(null); viewholder.getimageview().setimagedrawable(null); viewholder.getimageview().setimageresource(0); viewholder.getimageview().setlayoutparams(params); } }
in recreate params in xml doesn't work. method isimage()
checks mimetype of object.
can on this? it's frustrating.
any on appreciated.
edit adapter added
public class contentfeedadapter extends recyclerview.adapter<abstractholder> { private list<ifeedcontent> mfeedcontentlist; private context mcontext; private activity mmainactivity; private userhomefragment muserhomefragment; private userstreamfragment muserstreamfragment; private abstractholder mabstractholder; private final map<youtubethumbnailview, youtubethumbnailloader> mthumbnailviewtoloadermap; private arraylist<mymediaplayer> mmediaplayerlist = new arraylist<>(); public contentfeedadapter(context ctx, list<ifeedcontent> contentlist, activity mainactivity, userhomefragment userhomefragment, userstreamfragment userstreamfragment){ this.mcontext = ctx; this.mfeedcontentlist = contentlist; this.mmainactivity = mainactivity; this.mthumbnailviewtoloadermap = new hashmap<youtubethumbnailview, youtubethumbnailloader>(); this.muserhomefragment = userhomefragment; this.muserstreamfragment = userstreamfragment; } @override public abstractholder oncreateviewholder(viewgroup parent, int viewtype) { mabstractholder = createabstractholder(viewtype, parent); return mabstractholder; } @override public void onbindviewholder(final abstractholder holder, final int position) { final ifeedcontent content = mfeedcontentlist.get(position); holder.binddata(content); if((content.getmedia()!=null) && !content.getmedia().isempty()){ string mimetype = mediautils.getmedia(content).getmimetype(); if(mimetype.contains(mcontext.getstring(r.string.video)) || mimetype.contains(mcontext.getstring(r.string.audio)) && !mimetype.contains(mcontext.getstring(r.string.youtube))){ final progressbar progressbar = holder.getprogress(); final imageview playbutton = holder.getplayimage(); final button retrybutton = holder.getretryimage(); final relativelayout playeroverlay = holder.getplayeroverlay(); final imageview mediathumb = holder.getmediathumbnail(); final mymediaplayer player = new mymediaplayer(mcontext, holder.gettextureview(), holder.getmediacontrolleranchor(), holder.getprogress(), mimetype, myconstants.seek_to_default, retrybutton, playbutton, playeroverlay, mediathumb); player.setrecyclerviewposition(position); mmediaplayerlist.add(player); playbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { player.startvideo(mediautils.getmedia(content).getmediaurl()); holder.getplayimage().setvisibility(view.gone); progressbar.setvisibility(view.visible); } }); } } } /** * release holders used * thumbnail views */ public void releaseyoutubeholders(){ mabstractholder.releaseholders(); } @override public int getitemviewtype(int position){ int viewtype = -1; //instantiate viewholder utils // viewtype = viewholderutils.selectviewholder(mfeedcontentlist.get(position)); return viewtype; } @override public int getitemcount() { return mfeedcontentlist.size(); } @override public void onviewrecycled(abstractholder viewholder){ super.onviewrecycled(viewholder); int position = viewholder.getadapterposition(); ifeedcontent content = mfeedcontentlist.get(position); linearlayout.layoutparams params = new linearlayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content); params.setmargins(0, (int) utils.dptopx(mcontext,10),0,0); params.gravity = gravity.center; if(isimage(content)){ viewholder.getimageview().setimageuri(null); viewholder.getimageview().setimagedrawable(null); viewholder.getimageview().setimageresource(0); viewholder.getimageview().setlayoutparams(params); } } /** * create instance of * compatible viewholder * * @param viewtype * @param parent * @return */ private abstractholder createabstractholder(int viewtype, viewgroup parent) { abstractholder holder = null; switch (viewtype) { case myconstants.holder_type_1: holder = viewholder_var1.create(parent, muserhomefragment, muserstreamfragment); break; case myconstants.holder_type_2: holder = viewholder_var2.create(parent, muserhomefragment, muserstreamfragment); break; case myconstants.holder_type_3: holder = viewholder_var3.create(parent, muserhomefragment, muserstreamfragment); l.i(getclass().getsimplename(), "holder 3"); //holder.setisrecyclable(false); break; case myconstants.holder_type_4: holder = viewholder_var4.create(parent, muserhomefragment, muserstreamfragment); l.i(getclass().getsimplename(), "holder 4"); break; case myconstants.holder_type_5: holder = viewholder_var5.create(parent, muserhomefragment, muserstreamfragment); l.i(getclass().getsimplename(), "holder 5"); break; case myconstants.holder_type_6: holder = viewholder_var6.create(parent, muserhomefragment, muserstreamfragment); l.i(getclass().getsimplename(), "holder 6"); break; case myconstants.holder_type_7: holder = viewholder_var7.create(parent, muserhomefragment, muserstreamfragment); l.i(getclass().getsimplename(), "holder 7"); break; case myconstants.holder_type_8: holder = viewholder_var8.create(parent, muserhomefragment, muserstreamfragment); l.i(getclass().getsimplename(), "holder 8"); break; case myconstants.holder_type_9: holder = viewholder_var9.create(parent, muserhomefragment, muserstreamfragment); break; case myconstants.holder_type_10: holder = viewholder_var10.create(parent, mthumbnailviewtoloadermap, muserhomefragment, muserstreamfragment); } return holder; } private boolean isimage(ifeedcontent contentitem) { if (mediautils.getmedia(contentitem) != null) { string mimetype = mediautils.getmedia(contentitem).getmimetype(); if (mimetype.contains("image")) { l.i(getclass().getsimplename(), "image here"); return true; } else { l.i(getclass().getsimplename(), "no image here"); } } return false; } }
edit 2 viewholder 3
public class viewholder_var3 extends abstractholder { @bind(r.id.text_holder1) textview heading; @bind(r.id.text_holder2) textview body; @bind(r.id.image)imageview image; @bind(r.id.tabs_layout)linearlayout tabslayout; @bind(r.id.hot)textview hot; @bind(r.id.comments)textview children; @bind(r.id.gif_label)textview giftag; @bind(r.id.user_name)textview username; @bind(r.id.tag1)textview tag1; @bind(r.id.tag2)textview tag2; @bind(r.id.tag3)textview tag3; @bind(r.id.profile_pic) simpledraweeview profilepic; private boolean mellipsize; private boolean mexpanded; private userhomefragment muserhomefragment; private userstreamfragment muserstreamfragment; public viewholder_var3(view itemview, userhomefragment userhomefragment, userstreamfragment userstreamfragment) { super(itemview); butterknife.bind(this, itemview); muserhomefragment = userhomefragment; this.muserstreamfragment = userstreamfragment; } @override public void binddata(final ifeedcontent feedcontent) { username.settext(feedcontent.getauthor().getdisplayname()); image.setimageresource(0); image.setimagedrawable(null); image.setimageuri(null); textview [] tagsarray = {tag1, tag2, tag3}; if (feedcontent.getname() != null) { heading.settext(feedcontent.getname()); } else { heading.settext(feedcontent.geturl()); } if (feedcontent.getname() != null) { body.settext((feedcontent.getmessage())); } else { body.settext(feedcontent.geturl()); } log.i(viewholder_var3.class.getsimplename(), "number of lines: " + string.valueof(body.getlinecount())); if(!mediautils.getmedia(feedcontent).getmimetype().equals("image/gif")){ giftag.setvisibility(view.gone); glide.with(itemview.getcontext()).load(uri.parse(mediautils.getmedia(feedcontent).getmediaurl())).placeholder(r.drawable.placeholder).diskcachestrategy(diskcachestrategy.source).crossfade().into(image); }else { glide.with(itemview.getcontext()).load(uri.parse(mediautils.getmedia(feedcontent).getmediaurl())).asgif().placeholder(r.drawable.placeholder).diskcachestrategy(diskcachestrategy.result).crossfade().into(image); } displayprofilepic(feedcontent, profilepic); glide.with(itemview.getcontext()).load(uri.parse(mediautils.getmedia(feedcontent).getmediaurl())).placeholder(r.drawable.placeholder).diskcachestrategy(diskcachestrategy.all).crossfade().into(image); if(muserhomefragment==null){ username.setenabled(false); profilepic.setenabled(false); }else{ username.setenabled(true); profilepic.setenabled(true); } username.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { startactivityforresult(muserhomefragment, feedcontent.getauthor().getid(), feedcontent.getparentid()); } }); profilepic.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { startactivityforresult(muserhomefragment, feedcontent.getauthor().getid(), feedcontent.getparentid()); } }); long hotamt = feedcontent.getlikecount() - feedcontent.getdislikecount(); hot.settext(string.valueof(hotamt)); children.settext(string.valueof(feedcontent.getchildcount())); list<string> tagslist = feedcontent.gettags(); populatetags(tagslist, tagsarray); // if (feedcontent.gettags().size() > 0) addtags(tags, tabslayout); viewtreeobserver vto = body.getviewtreeobserver(); vto.addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { viewtreeobserver obs = body.getviewtreeobserver(); obs.removeongloballayoutlistener(this); layout layout = body.getlayout(); if(layout!=null){ int lines = layout.getlinecount(); if(lines>0){ if(layout.getellipsiscount(lines-1)>0){ mellipsize = true; } } } } }); body.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (mellipsize) { if (!mexpanded) { objectanimator animation = objectanimator.ofint(body, "maxlines", 20); //animation.setinterpolator(new bounceinterpolator()); animation.setduration(200).start(); // toast.maketext(itemview.getcontext(), "i clicked", toast.length_long).show(); mexpanded = true; } else { objectanimator animation = objectanimator.ofint(body, "maxlines", 4); //animation.setinterpolator(new bounceinterpolator()); animation.setduration(200).start(); // toast.maketext(itemview.getcontext(), "i clicked", toast.length_long).show(); mexpanded = false; } } } }); } @override public imageview getimageview(){ return image; } public static viewholder_var3 create(viewgroup parent, userhomefragment homefragment, userstreamfragment userstreamfragment){ view root = layoutinflater.from(parent.getcontext()).inflate(r.layout.feed_content_item_layout_var3, parent, false); return new viewholder_var3(root, homefragment, userstreamfragment); } }
add line
android:adjustviewbounds="true"
to imageview
in layout file automatically resize image view.
in glide change .crossfade()
.fitcenter()
Comments
Post a Comment