How to destroy an unselect tab? or replace the current tab with a new one? TABLAYOUT android -
i´m trying delete/remake/replace or destroy tabs aren´t selected. can reload/create tab , content refreshed. since onresume doesn´t work on tablayout.
i´m using tablayout here mainactivity code:
public class mainactivity extends appcompatactivity{ // declaring view , variables toolbar toolbar; viewpager viewpager; viewpageradapter adapter; tablayout tablayout; charsequence titles[]={"participantes","torneio","classificação"}; int numboftabs =3; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar = (toolbar) findviewbyid(r.id.tool_bar); viewpager = (viewpager) findviewbyid(r.id.viewpager); tablayout = (tablayout)findviewbyid(r.id.tablayout); // creating toolbar , setting toolbar activity setsupportactionbar(toolbar); // creating viewpageradapter , passing fragment manager, titles fot tabs , number of tabs. adapter = new viewpageradapter(getsupportfragmentmanager(),titles,numboftabs); // assigning viewpager view , setting adapter viewpager.setadapter(adapter); tablayout.setupwithviewpager(viewpager); tablayout.setselectedtabindicatorcolor(getresources().getcolor(r.color.colortexticons)); tablayout.setontabselectedlistener(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) { } }); } @override protected void ondestroy() { super.ondestroy(); viewpager = null; } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu, 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; } if (id == r.id.action_info) { toast.maketext(getapplicationcontext(), "contacto: juniortalisson16@gmail.com", toast.length_long).show(); return true; } return super.onoptionsitemselected(item); } }
my adapter code:
/** * created talisson on 03-04-2016. */ public class viewpageradapter extends fragmentstatepageradapter { charsequence titles[]; // store titles of tabs going passed when viewpageradapter created int numboftabs; // store number of tabs, passed when viewpageradapter created // build constructor , assign passed values appropriate values in class public viewpageradapter(fragmentmanager fm,charsequence mtitles[], int mnumboftabsumb) { super(fm); this.titles = mtitles; this.numboftabs = mnumboftabsumb; } @override public void destroyitem(viewgroup container, int position, object object) { super.destroyitem(container, position, object); fragmentmanager manager = ((fragment) object).getfragmentmanager(); fragmenttransaction trans = manager.begintransaction(); trans.remove((fragment) object); trans.commit(); } //this method return fragment every position in view pager @override public fragment getitem(int position) { switch(position) {// if position 0 returning first tab case 0: tab1 tab1 = new tab1(); return tab1; case 1: tab2 tab2 = new tab2(); return tab2; case 2: tab3 tab3 = new tab3(); return tab3; default: return null; } } // method return titles tabs in tab strip @override public charsequence getpagetitle(int position) { return titles[position]; } // method return number of tabs tabs strip @override public int getcount() { return numboftabs; } }
Comments
Post a Comment