Android OnClickListener for RecyclerView's Child's Child -
what want set onclicklistener imageview in item in recyclerview. want click on imageview code , click anywhere else run code b. how can that? using:
public class recycleradapter extends recyclerview.adapter<recycleradapter.recyclerviewholder> { layoutinflater inflater; context context; clicklistener clicklistener; list<postcontent> posts = collections.emptylist(); public recycleradapter(context context) { this.context = context; inflater = layoutinflater.from(context); } public void setposts(list<postcontent> posts) { this.posts = posts; } @override public recyclerviewholder oncreateviewholder(viewgroup viewgroup, int i) { view view = inflater.inflate(r.layout.post_row, viewgroup, false); recyclerviewholder viewholder = new recyclerviewholder(view); return viewholder; } @override public void onbindviewholder(recyclerviewholder viewholder, int i) { viewholder.name.settext(posts.get(i).name); viewholder.username.settext("@" + posts.get(i).username); viewholder.time_post.settext(posts.get(i).time_post); viewholder.content.settext(posts.get(i).content); } @override public int getitemcount() { return posts.size(); } public void setclicklistener(clicklistener clicklistener) { this.clicklistener = clicklistener; } public interface clicklistener { public void itemclicked(view view, int pos); } public class recyclerviewholder extends recyclerview.viewholder implements view.onclicklistener { textview name; textview username; textview time_post; textview content; imageview profile; public recyclerviewholder(view itemview) { super(itemview); name = (textview) itemview.findviewbyid(r.id.name); username = (textview) itemview.findviewbyid(r.id.username); time_post = (textview) itemview.findviewbyid(r.id.time); content = (textview) itemview.findviewbyid(r.id.content); profile = (imageview) itemview.findviewbyid(r.id.profile); itemview.setonclicklistener(this); } @override public void onclick(view v) { if (clicklistener != null) { clicklistener.itemclicked(v, getposition()); } } } }
for adapter , view holder.
okay might not correct way it, how got work.
firstly create click interface in viewholder:
private tasklistrecyclerviewclickinterface clicklistener;
...
this.clicklistener = clicklistener;
...
public interface tasklistrecyclerviewclickinterface { public void onitemclicked(int position, string tag); }
then add onclicklistener views want , call interface when these items clicked so:
holder_view.settag("holder"); holder_view.setonclicklistener(this); imageview.settag("imageview"); imageview.setonclicklistener(this);
and onclicklistener :
@override public void onclick(view v) { if (clicklistener != null) { clicklistener.onitemclicked(getposition(), v.gettag().tostring()); } }
now activity, pass interface adapter in constructor , pass interface viewholder so:
public inboxtasklistadapter(list<items> items, inboxtasksviewholder.tasklistrecyclerviewclickinterface clicklistener) { this.items = items; this.clicklistener = clicklistener; }
in oncreateviewholder should pass interface viewholder.
now in activity or fragment implement listener , override onitemclicked method. view position , tag clicked on.
Comments
Post a Comment