onclicklistener - how to set on click listener of recyclerview where list items are heterogenous -
i have recyclerview 2 types of viewholders showing 2 types of items want implement on click listener on sub itmes in items existing solutions shows viewholder class should implemented static inside adapter ,but have declared them in sepearte file(a tutorial suggested), cant implement onlick listener on viewholders viewholder1 class
public class viewholder1 extends recyclerview.viewholder { private textview label1, label2; public imageview postimage; public viewholder1(view v) { super(v); label1 = (textview) v.findviewbyid(r.id.text11); label2 = (textview) v.findviewbyid(r.id.text22); postimage = (imageview) v.findviewbyid(r.id.dummyimage); } public textview getlabel1() { return label1; } public void setlabel1(textview label1) { this.label1 = label1; } public textview getlabel2() { return label2; } public void setlabel2(textview label2) { this.label2 = label2; } }
viewholder2 is
public class viewholder2 extends recyclerview.viewholder { private imageview ivexample; public viewholder2(view v) { super(v); ivexample = (imageview) v.findviewbyid(r.id.ivexample); } public imageview getimageview() { return ivexample; } public void setimageview(imageview ivexample) { this.ivexample = ivexample; } }
adapter
public class complexrecyclerviewadapter extends recyclerview.adapter<recyclerview.viewholder> { // items display in recyclerview private list<object> items; private final int user = 0, image = 1; // provide suitable constructor (depends on kind of dataset) public complexrecyclerviewadapter(list<object> items) { this.items = items; } // return size of dataset (invoked layout manager) @override public int getitemcount() { return this.items.size(); } @override public int getitemviewtype(int position) { if (items.get(position) instanceof user) { return user; } else if (items.get(position) instanceof string) { return image; } return -1; } @override public recyclerview.viewholder oncreateviewholder(viewgroup viewgroup, int viewtype) { recyclerview.viewholder viewholder; layoutinflater inflater = layoutinflater.from(viewgroup.getcontext()); switch (viewtype) { case user: view v1 = inflater.inflate(r.layout.layout_viewholder1, viewgroup, false); viewholder = new viewholder1(v1); break; case image: view v2 = inflater.inflate(r.layout.layout_viewholder2, viewgroup, false); viewholder = new viewholder2(v2); break; default: view v3 = inflater.inflate(r.layout.layout_viewholder2, viewgroup, false); viewholder = new viewholder2(v3); break; } return viewholder; } @override public void onbindviewholder(recyclerview.viewholder viewholder, final int position) { switch (viewholder.getitemviewtype()) { case user: viewholder1 vh1 = (viewholder1) viewholder; configureviewholder1(vh1, position); break; case image: viewholder2 vh2 = (viewholder2) viewholder; configureviewholder2(vh2); break; default: viewholder2 vh3 = (viewholder2) viewholder; configureviewholder2(vh3); break; } } // private void configuredefaultviewholder(recyclerviewsimpletextviewholder vh, int position) { // vh.getlabel().settext((charsequence) items.get(position)); // } private void configureviewholder1(viewholder1 vh1, int position) { user user = (user) items.get(position); if (user != null) { vh1.getlabel1().settext(user.getmfname()); vh1.getlabel2().settext( user.getmname()); //vh1.postimage.setimageresource(r.drawable.universe); picasso.with(vh1.postimage.getcontext()) .load(r.drawable.universe) .resize(400,200) // resizes image these dimensions (in pixel) .centercrop() .placeholder(r.drawable.progress_animation) .error(r.drawable.error_image) .into(vh1.postimage); } } private void configureviewholder2(viewholder2 vh2) { // vh2.getimageview().setimageresource(r.drawable.sample_golden_gate); picasso.with(vh2.getimageview().getcontext()) .load(r.drawable.sample_golden_gate) .resize(400,200) // resizes image these dimensions (in pixel) .centercrop() .placeholder(r.drawable.progress_animation) .error(r.drawable.error_image) .into(vh2.getimageview()); } }
fragment uses all
public class onefragment extends fragment { private static final int vertical_item_space = 48; button button; recyclerview rvmixed; public onefragment() { // required empty public constructor } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view rootview = inflater.inflate(r.layout.fragment_one, container, false); // lookup recyclerview in activity layout rvmixed = (recyclerview) rootview.findviewbyid(r.id.rvcontacts); rvmixed.setadapter(new complexrecyclerviewadapter(getsamplearraylist())); linearlayoutmanager layoutmanager = new linearlayoutmanager(getactivity().getapplicationcontext()); // control orientation of items // supports linearlayoutmanager.horizontal layoutmanager.setorientation(linearlayoutmanager.vertical); // optionally customize position want default scroll layoutmanager.scrolltoposition(0); // attach layout manager recyclerview rvmixed.setlayoutmanager(layoutmanager); //adding spacing between recycler view items // rvmixed.additemdecoration(new verticalspaceitemdecoration(vertical_item_space)); // that's all! return rootview; } private void binddatatoadapter() { // bind adapter recycler view object rvmixed = (recyclerview) getview().findviewbyid(r.id.rvcontacts); complexrecyclerviewadapter madapter = new complexrecyclerviewadapter(getsamplearraylist()); rvmixed.setadapter(madapter); } private arraylist<object> getsamplearraylist() { arraylist<object> items = new arraylist<>(); items.add(new user("dany targaryen", "valyria")); items.add(new user("rob stark", "winterfell")); items.add("image"); items.add(new user("jon snow", "castle black")); items.add("image"); items.add(new user("tyrion lanister", "king's landing")); return items; } }
you can pass click listener on complexrecyclerviewadapter through constructor @ time of initialization. , can reuse same click listener reference each of viewholder.