Issue in frgment replace in FragmentStatePagerAdapter tabhoast matireal design android -
i want integrate tabhoast material design android write below code
public class activitypaymentmodule extends appcompatactivity implements materialtablistener { private viewpager pager; private materialtabhost tabhost; private viewpageradapter pageradapter; private resources res; public static int tabposition = 0; private allmethods allmethods; private string titles[]={"tab1","tab2, "tab3","tab4"}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_payment_module); res = this.getresources(); allmethods = new allmethods(activitypaymentmodule.this); toolbar toolbar = (toolbar) this.findviewbyid(r.id.toolbar); toolbar.settitletextcolor(color.white); toolbar.settitle("demo"); this.setsupportactionbar(toolbar); initcontrols(); setpageradapter(); } private void initcontrols() { tabhost = (materialtabhost)findviewbyid(r.id.materialtabhost); pager = (viewpager) this.findviewbyid(r.id.pager); pageradapter = new viewpageradapter(getsupportfragmentmanager()); } private void setpageradapter() { pager.setadapter(pageradapter); tabhost.setaccentcolor(color.white); pager.setonpagechangelistener(new viewpager.simpleonpagechangelistener() { @override public void onpageselected(int position) { // when user swipe selected tab change tabposition = position; tabhost.setselectednavigationitem(position); } }); (int = 0; < pageradapter.getcount(); i++) { tabhost.addtab( tabhost.newtab() .settext(titles[i]) .settablistener(this) ); } } @override public void ontabselected(materialtab materialtab) { pager.setcurrentitem(materialtab.getposition()); } @override public void ontabreselected(materialtab materialtab) { } @override public void ontabunselected(materialtab materialtab) { } private class viewpageradapter extends fragmentstatepageradapter { public viewpageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { if (position == 0) { return new fragment1(); }else if (position == 1) { return new fragment2(); }else if(position == 2){ return new fragment3(); } else { return new fragment4(); } } @override public int getcount() { return 4; } @override public charsequence getpagetitle(int position) { return titles[position]; } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.income_menu, menu); menu.finditem(r.id.action_dasbboard).setvisible(false); menu.finditem(r.id.action_feedback).setvisible(false); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { // respond action bar's up/home button case r.id.terms: intent browserintent2 = new intent(intent.action_view, uri.parse("http://worldofrental.com/terms-of-service/")); startactivity(browserintent2); return true; case r.id.policy: intent browserintent3 = new intent(intent.action_view, uri.parse("http://worldofrental.com/privacy-policy/")); startactivity(browserintent3); case r.id.action_dasbboard: return true; case r.id.action_help: intent browserintent = new intent(intent.action_view, uri.parse("http://worldofrental.com/help-center/")); startactivity(browserintent); return true; } return super.onoptionsitemselected(item); } @override public void onbackpressed() { super.onbackpressed(); this.finish(); }
}
edit
i face 2 issues: (1) when first time load screen @ time fragment1() , fragment2() both called. (2)when swipe fragment @ time fragment replace position not maintain when swipe fragment1() right left @ time fragment3() , fragment2() both called
when run above code @ first time fragment1() , fragment2() both fragment called. , when swipe fragment position not maintain replacing idea how can solve ? suggestions appreciable.
Comments
Post a Comment