android - How to swipe between Fragments with FragmentPagerAdapter -
i can't figure out why swiping between fragments isn't working,
here custom fragmentpageradapter
:
public class tabspageradapter extends fragmentpageradapter { public tabspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int index) { switch (index) { case 0: return new prearrivalplan(); case 1: return new primarysurvey(); case 2: return new vitals(); case 3: return new secondarysurvey(); case 4: return new preparefortravel(); } return null; } @override public int getcount() { // item count - equal number of tabs return 5; }
}
and here activity uses it:
public class mainactivity extends fragmentactivity implements actionbar.tablistener { private customviewpager viewpager; private tabspageradapter tabspageradapter; private actionbar actionbar; private string[] tabtitles = {"pre-arrival plan", "primary survey", "vitals", "secondary survey", "prepare travel"}; private int totaltabs = 5; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); viewpager = (customviewpager) findviewbyid(r.id.pager); actionbar = getactionbar(); tabspageradapter = new tabspageradapter(getsupportfragmentmanager()); viewpager.setadapter(tabspageradapter); viewpager.setpagingenabled(false); actionbar.sethomebuttonenabled(true); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); viewpager.setonpagechangelistener(new customviewpager.onpagechangelistener() { @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { } @override public void onpageselected(int position) { actionbar.setselectednavigationitem(position); } @override public void onpagescrollstatechanged(int state) { } }); viewpager.setoffscreenpagelimit(totaltabs); // remove android icon action bar getactionbar().seticon(new colordrawable(getresources().getcolor(android.r.color.transparent))); getactionbar().setdisplayshowhomeenabled(false); // add tabs action bar (string tab_name : tabtitles) { spannablestring s = new spannablestring(tab_name); s.setspan(new typefacespan("helveticaneuelight.ttf"), 0, s.length(), spannable.span_exclusive_exclusive); actionbar.addtab(actionbar.newtab().settext(s).settablistener(this)); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_main, menu); return true; } public void updatetabtitles(int tabnumber, int checkboxesremaining) { string text = tabtitles[tabnumber] + "\n (" + checkboxesremaining + " unchecked)"; actionbar.gettabat(tabnumber).settext(text); } @override public void ontabselected(actionbar.tab tab, fragmenttransaction ft) { viewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(actionbar.tab tab, fragmenttransaction ft) { } @override public void ontabreselected(actionbar.tab tab, fragmenttransaction ft) { } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: intent intent = new intent(this, mainmenu.class); startactivity(intent); return true; case r.id.complete: dialoginterface.onclicklistener dialogclicklistener = new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { switch (which){ case dialoginterface.button_positive: gotoreport(); break; case dialoginterface.button_negative: // "no" button clicked, nothing break; } } }; alertdialog.builder builder = new alertdialog.builder(this); builder.setmessage("are sure want complete checklist?").setpositivebutton("yes", dialogclicklistener).setnegativebutton("no", dialogclicklistener).show(); return true; default: return super.onoptionsitemselected(item); } } public void gotoreport() { intent intent = new intent(this, report.class); startactivity(intent); } };
could viewpager.setpagingenabled(false);
method? try taking method out, or @ least setting true.
Comments
Post a Comment