android - Tablayout tab with multiple fragment -
i working on app use tab layout 5 tabs. need navigate multiple fragment inside every tab, , open first fragment when user again reselected tab. please me.
tablayout tablayout = (tablayout) findviewbyid(r.id.tablayout); tablayout.addtab(tablayout.newtab().seticon(r.drawable.scene_tab_selector)); tablayout.addtab(tablayout.newtab().seticon(r.drawable.my_scene_tab_selector)); tablayout.addtab(tablayout.newtab().seticon(r.drawable.conversation_tab_selector)); tablayout.addtab(tablayout.newtab().seticon(r.drawable.notification_tab_selector)); tablayout.addtab(tablayout.newtab().seticon(r.drawable.more_tab_selector)); tablayout.settabgravity(tablayout.gravity_fill); final viewpager viewpager = (viewpager) findviewbyid(r.id.pager); final pageradaptor adapter = new pageradaptor (getsupportfragmentmanager(), tablayout.gettabcount()); viewpager.setadapter(adapter); viewpager.addonpagechangelistener(new tablayout.tablayoutonpagechangelistener(tablayout)); tablayout.setontabselectedlistener(new tablayout.ontabselectedlistener() { @override public void ontabselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition()); tab.geticon().setcolorfilter(color.white, porterduff.mode.src_in); if(tab.getposition()==4) { // pagetitlelabel.settext("my profile"); } } @override public void ontabunselected(tablayout.tab tab) { tab.geticon().setcolorfilter(color.parsecolor("#a8a8a8"), porterduff.mode.src_in); } @override public void ontabreselected(tablayout.tab tab) { } });
my pageradaptor follows
public class pageradaptor extends fragmentstatepageradapter { int mnumoftabs; public pageradaptor(fragmentmanager fm, int numoftabs) { super(fm); this.mnumoftabs = numoftabs; } @override public fragment getitem(int position) { switch (position) { case 0: tab1containerfragment tab1 = new tab1containerfragment(); return tab1; case 1: mycuddllfragment tab2 = new mycuddllfragment(); return tab2; case 2: // cuddllconversationfragment tab3 = new cuddllconversationfragment(); conversationfragment conversationfragment = new conversationfragment(); return conversationfragment; case 3: // cuddllnotificationsfragment tab4 = new cuddllnotificationsfragment(); tab4containerfragment notificationpagefragment = new tab4containerfragment(); return notificationpagefragment; case 4: myprofilefragment tab5 = new myprofilefragment(); return tab5; default: return null; } } @override public int getcount() { return mnumoftabs; } }
public void ontabselected(tablayout.tab tab) { tab.geticon().setcolorfilter(color.white, porterduff.mode.src_in); if(tab.getposition()==0) { viewpager.setcurrentitem(0); }else if(tab.getposition()==1) { viewpager.setcurrentitem(1); }else if(tab.getposition()==2) { viewpager.setcurrentitem(2); }else if(tab.getposition()==3) { viewpager.setcurrentitem(3); } }
Comments
Post a Comment