android - How to pass data to fragment when click on cardview in onBindViewHolder? -


here adapter code , want pass data "particularfragment" note : particularfragment extends fragment

public class topicsadapter extends recyclerview.adapter<topicsadapter.viewholder> {      private context context;     private arraylist<quoteitems> itemlist;      public topicsadapter(context context, arraylist<quoteitems> itemlist) {         this.context = context;         this.itemlist = itemlist;     }      @override     public topicsadapter.viewholder oncreateviewholder(viewgroup viewgroup, int i) {         view v = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.adapter_topics_recycler_view_items, viewgroup, false);         viewholder viewholder = new viewholder(v);          return viewholder;     }      @override     public void onbindviewholder(final topicsadapter.viewholder viewholder, int i) {          final quoteitems quoteitems = itemlist.get(i);         viewholder.txtquote.settext(quoteitems.getquote());          viewholder.cardview.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {               //how pass data "particularfragment" here             }         });     }      @override     public int getitemcount() {         return itemlist.size();     }     class viewholder extends recyclerview.viewholder {          public textview txtquote;         public cardview cardview;          public viewholder(view itemview) {             super(itemview);             txtquote = (textview) itemview.findviewbyid(r.id.txtquote);             cardview = (cardview) itemview.findviewbyid(r.id.cardview);         }     } } 

add following code in onclick

reciverfragment fragment = new reciverfragment();  bundle args = new bundle(); args.putstring("your_key", "your_value"); fragment.setarguments(args); //inflate fragment context.getfragmentmanager().begintransaction().add(r.id.container, fragment).commit(); 

note: change

private context context; 

to

private activity context; 

and parameter of topicsadapter constructor pass getactivity() fragment.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -