android - How to start fragment from a specific position and be able to go to previous positions? -
i have listview. when click on element of listview pass 2 string arrays , list position. use information start pager of fragments. working fine. information, displayed. let's click on listitem-5, new fragment start @ 5 , i'll able go 6,7,8 etc., won't able go left @ 4,3,2,1.how can achieve ?
public class viewpagerfragment extends fragmentactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_view_pager_fragment); arraylist<string> email=getintent().getextras().getstringarraylist("email"); arraylist<string> about=getintent().getextras().getstringarraylist("about"); int listposition = getintent().getextras().getint("position"); viewpager vppager = (viewpager) findviewbyid(r.id.vppager); adapterviewpager = new mypageradapter(getsupportfragmentmanager(),email,about,listposition); vppager.setadapter(adapterviewpager); } private smartfragmentstatepageradapter adapterviewpager; // extend smartfragmentstatepageradapter instead more dynamic viewpager items public static class mypageradapter extends smartfragmentstatepageradapter { private final arraylist<string> email; private final arraylist<string> about; private int listposition; public mypageradapter(fragmentmanager fragmentmanager,arraylist<string> email,arraylist<string> about,int lposition) { super(fragmentmanager); this.email=email; this.about = about; listposition = lposition; } // returns total number of pages @override public int getcount() { return email.size(); } // returns fragment display page @override public fragment getitem(int position) { return firstfragment.newinstance(email.get(listposition+position), about.get(listposition+position)); } // returns page title top indicator @override public charsequence getpagetitle(int position) { return "page " + position; } } }
i not understanding code design. allow me understand sample scenario. if click on listitem-5, perhaps used code:
adapterviewpager.getitem(0);
if need see fragment 4, need go backwards. hence use code:
adapterviewpager.getitem(-1);
otherwise, use code:
adapterviewpager = new mypageradapter(getsupportfragmentmanager(),email,about, 0);
note value 0 4th parameter start first fragment.
Comments
Post a Comment