android - Static ViewHolder and getting context when using with RecyclerView -
i'm trying use recycler view , handle on click event. i've read various methods of handling onclick event on recycler view item :
- define click listener inside view holder class itself.
- define click listener in oncreateviewholder().
- define interface , go there (seems work).
so first question option better? i'm using first method , if defining click listener in view holder class way go, how use context adapter view holder class static.
basically, want have static view holder , on click event, open new activity need context.
update : adding adapter , viewholder code.
public class myadapter extends recyclerview.adapter<myadapter.viewholder> { private context mcontext; private list<job> jobs; public myadapter(context context, list<job> jobs) { mcontext = context; this.jobs = jobs; } @override public myadapter.viewholder oncreateviewholder(viewgroup viewgroup, int i) { view itemlayoutview = layoutinflater.from(viewgroup.getcontext()) .inflate(r.layout.list_item, viewgroup, false); itemlayoutview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(mcontext, myactivity.class); mcontext.startactivity(intent); } }); return new viewholder(itemlayoutview); } @override public void onbindviewholder(workexperienceadapter.viewholder viewholder, int i) { //bindviewholder code } } @override public int getitemcount() { return jobs.size(); } public static class viewholder extends recyclerview.viewholder { @injectview(r.id.current) textview mcurrent; public viewholder(view itemview) { super(itemview); butterknife.inject(this, itemview); } }
}
in view holder constructor object of view class. can use object context like:
class holder extends recyclerview.viewholder { public holder(view itemview) { super(itemview); context context = itemview.getcontext(); } }
Comments
Post a Comment