android - How to set tab at position 1 in viewpager -
i've 2 tabs. default 0's position tab selected wan select tab @ 1st position. i've tried
tablayout tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.tab tab = tablayout.gettabat(someindex); tab.select();
also didn't work. search on stackoverflow , apply @ position 1 app crashed.
tablayout = (tablayout) findviewbyid(r.id.tab_layout); tablayout.addtab(tablayout.newtab().settext("add expense")); tablayout.addtab(tablayout.newtab().settext("add ledger")); final viewpager viewpager = (viewpager) findviewbyid(r.id.pager2); final tabadapter adapter = new tabadapter(getsupportfragmentmanager(), tablayout.gettabcount()); viewpager.setadapter(adapter); viewpager.addonpagechangelistener(new tablayout.tablayoutonpagechangelistener(tablayout)); tablayout.gettabat(1).select(); // } tablayout.addontabselectedlistener (new tablayout.ontabselectedlistener() { @override public void ontabselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(tablayout.tab tab) { } @override public void ontabreselected(tablayout.tab tab) { } }); }
tabadapter:
class tabadapter extends fragmentstatepageradapter { private int mnumoftabs; tabadapter(fragmentmanager fm, int numoftabs) { super(fm); this.mnumoftabs = numoftabs; } @override public fragment getitem(int position) { switch (position) { case 0: return new addexpensefragment(); case 1: return new addledgerfragment(); } return null; } @override public int getcount() { return mnumoftabs; } }
try viewpager.setcurrentitem(pos);
Comments
Post a Comment