android - How to open different activity by clicking on different cards in a recycler view with cards -


i have created recyclerview , added cards.i want open different activity clicking on different cards.

my codes are:

mainactivity

public class mainactivity extends appcompatactivity {     recyclerview mrecyclerview;     recyclerview.layoutmanager mlayoutmanager;     recyclerview.adapter madapter;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          mrecyclerview = (recyclerview)findviewbyid(r.id.recycler_view);         mrecyclerview.sethasfixedsize(true);          mlayoutmanager = new linearlayoutmanager(this);         mrecyclerview.setlayoutmanager(mlayoutmanager);          madapter = new cardadapter();         mrecyclerview.setadapter(madapter); }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     } } 

cardadapter

public class cardadapter extends recyclerview.adapter<cardadapter.viewholder> {      list<natureitem> mitems;      public cardadapter() {         super();         mitems = new arraylist<natureitem>();         natureitem nature = new natureitem();         nature.setname("the great barrier reef");         nature.setdes("lorem ipsum dolor sit amet, consectetur adipiscing elit, sed eiusmod tempor incididunt" +                 "ut labore et dolore magna aliqua. ut enim ad minim veniam.");         nature.setthumbnail(r.drawable.tag_logo);         mitems.add(nature);          nature = new natureitem();         nature.setname("grand canyon");         nature.setdes("lorem ipsum dolor sit amet, consectetur adipiscing elit, sed eiusmod tempor incididunt" +                 "ut labore et dolore magna aliqua.");         nature.setthumbnail(r.drawable.tag_logo);         mitems.add(nature);          nature = new natureitem();         nature.setname("baltoro glacier");         nature.setdes("lorem ipsum dolor sit amet, consectetur adipiscing elit, sed eiusmod tempor incididunt" +                 "ut labore et dolore magna aliqua. ut enim ad minim veniam, quis.");         nature.setthumbnail(r.drawable.tag_logo);         mitems.add(nature);          nature = new natureitem();         nature.setname("iguazu falls");         nature.setdes("lorem ipsum dolor sit amet, consectetur adipiscing elit, sed eiusmod tempor incididunt" +                 "ut labore et dolore magna aliqua. ut enim ad minim veniam.");         nature.setthumbnail(r.drawable.tag_logo);         mitems.add(nature);           nature = new natureitem();         nature.setname("aurora borealis");         nature.setdes("lorem ipsum dolor sit amet, consectetur adipiscing elit, sed eiusmod tempor incididunt" +                 "ut labore et dolore magna aliqua. ut enim ad minim veniam, quis nostrud.");         nature.setthumbnail(r.drawable.tag_logo);         mitems.add(nature);     }      @override     public viewholder oncreateviewholder(viewgroup viewgroup, int i) {         view v = layoutinflater.from(viewgroup.getcontext())                 .inflate(r.layout.recycler_view_card_item, viewgroup, false);         viewholder viewholder = new viewholder(v);         return viewholder;     }      @override     public void onbindviewholder(viewholder viewholder, int i) {         natureitem nature = mitems.get(i);         viewholder.tvnature.settext(nature.getname());         viewholder.tvdesnature.settext(nature.getdes());         viewholder.imgthumbnail.setimageresource(nature.getthumbnail());     }      @override     public int getitemcount() {         return mitems.size();     }      class viewholder extends recyclerview.viewholder {          public imageview imgthumbnail;         public textview tvnature;         public textview tvdesnature;          public viewholder(view itemview) {             super(itemview);             imgthumbnail = (imageview) itemview.findviewbyid(r.id.img_thumbnail);             tvnature = (textview) itemview.findviewbyid(r.id.tv_nature);             tvdesnature = (textview) itemview.findviewbyid(r.id.tv_des_nature);         }     } } 

natureitem

public class natureitem {     private string mname;     private string mdes;     private int mthumbnail;      public string getname() {         return mname;     }      public void setname(string name) {         this.mname = name;     }      public string getdes() {         return mdes;     }      public void setdes(string des) {         this.mdes = des;     }      public int getthumbnail() {         return mthumbnail;     }      public void setthumbnail(int thumbnail) {         this.mthumbnail = thumbnail;     } } 

xml files normal. recyclerview in activity_main.xml , 1 xml card view

create listener each "card" specified here.

as opening different activity different cards suggest each card (in case "natureitem") keep class of activity should started.

you able do:

intent intent = new intent(context, nature.getclass()); context.startactivity(intent); 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -