android - RecyclerView - CardViews overlap each other when scrolling (How to add multiple rows to a single CardView?) -
the entire layout set - recyclerview contains several card views, , each cardview can have multiple rows of linearlayouts of exact type (but different data).
everything works okay, except when scroll down or up, cards begins overlap on other cards randomly.
here's code:
public class myadapter extends recyclerview.adapter<myadapter.viewholder> implements view.onclicklistener{ context context; public myadapter(context context) { this.context = context; } @override public int getitemcount() { return 20; } @override public void onbindviewholder(viewholder holder, int position) { try { linearlayout.layoutparams contentparams = new linearlayout.layoutparams(100, 100); layoutinflater inflater = layoutinflater.from(context); linearlayout ll_parent = (linearlayout) inflater.inflate(r.layout.parent_linearlayout, null, false); networkimageview networkimageview = new networkimageview(context); string url = "https://www.google.co.in/images/branding/product/ico/googleg_lodp.png"; networkimageview.setimageurl(url, volleysingleton.getinstance(context).getimageloader()); networkimageview.setlayoutparams(contentparams); networkimageview.setpadding(20, 20, 20, 20); textview tv_card_title = new textview(context); tv_card_title.setpadding(20, 20, 20, 20); linearlayout ll_title = new linearlayout(context); ll_title.setorientation(linearlayout.horizontal); ll_title.addview(networkimageview); ll_title.addview(tv_card_title); ll_title.setpadding(20, 20, 20, 20); ll_parent.addview(ll_title); (int j = 0; j < getitemcount(); j++) { linearlayout ll_children = (linearlayout) inflater.inflate(r.layout.child_views, null, false); button bt_delete = (button) ll_children.findviewbyid(r.id.bt_delete); bt_delete.settag("card no: " + (position+1) + " iteration: " + (j+1)); bt_delete.setonclicklistener(this); button bt_edit = (button) ll_children.findviewbyid(r.id.bt_edit); bt_edit.settag("card no: " + (position + 1) + " iteration: " + (j + 1)); bt_edit.setonclicklistener(this); textview tv_abc = (textview)ll_children.findviewbyid(r.id.tv_abc); tv_abc.settext("abc"); tv_abc.settypeface(null, typeface.bold); tv_card_title.settext("abc"); tv_card_title.settypeface(null, typeface.bold); networkimageview iv_image = (networkimageview)ll_children.findviewbyid(r.id.iv_image); iv_profileimage.setimageurl(url, volleysingleton.getinstance(context).getimageloader()); ll_parent.addview(ll_children); } holder.cardview.addview(ll_parent); } catch (jsonexception e) { e.printstacktrace(); } } @override public viewholder oncreateviewholder(viewgroup viewgroup, int i) { view itemview = layoutinflater. from(viewgroup.getcontext()). inflate(r.layout.grandparent_cardview, viewgroup, false); return new viewholder(itemview); } @override public void onclick(view v) { switch (v.getid()) { case r.id.bt_delete: break; case r.id.bt_edit: break; } } public static class viewholder extends recyclerview.viewholder { cardview cardview; public viewholder(view v) { super(v); cardview = (cardview) v.findviewbyid(r.id.cv); } } }
what's wrong code above? there better way add multiple rows (of same type) single cardview?
one thing might want check layout parameters. have 3-tier view hierarchy , things can jumbled if layout parameters aren't set "match_parent" , "wrap_content" in appropriate places. suggest rewriting layouts in xml , experimenting layout_width , layout_height parameters.
Comments
Post a Comment