android - Problems with Fragment and ViewPager manage -
i trying create activity manage dynamic creation of fragments
in viewpager
... problem i'm having problems manage them.
the fragments
should created @ start of activity , if swipe left or right, not create them second time show me correct one.
package info.androidhive.materialdesign.activity; import android.app.activity; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.app.fragmentstatepageradapter; import android.support.v4.view.viewpager; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import java.util.arraylist; import java.util.list; import info.androidhive.materialdesign.r; import info.androidhive.materialdesign.utility.mydatacontainer; import xdroid.toaster.toaster; public class screenslidepageractivity extends fragment { /** * number of pages (wizard steps) show in demo. */ public int num_pages = mydatacontainer.getnumberofstreets(mydatacontainer.allstreets) + 1; static int mpagelastscreen=0; static int currentpage = 0 ; /** * pager widget, handles animation , allows swiping horizontally access previous * , next wizard steps. */ private viewpager mpager; private list<fragment> fragments = new arraylist<fragment>(); /** * pager adapter, provides pages view pager widget. */ private viewpageradapter mpageradapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //setcontentview(r.layout.activity_screen_slide); for(int x = 0; x < num_pages ; x++) { fragments.add(new screenslidepagefragment()); } } public void onresume(){ super.onresume(); if (!(mpageradapter == null)) { mpageradapter.notifydatasetchanged(); } } public void onviewcreated(view v,bundle savedinstancestate){ super.onviewcreated(v, savedinstancestate); mpager = (viewpager) v.findviewbyid(r.id.pager); mpageradapter = new viewpageradapter(getfragmentmanager(),fragments); mpager.setadapter(mpageradapter); mpager.setonpagechangelistener(new viewpager.onpagechangelistener() { private float thresholdoffset = 0.5f; private boolean firstplus = true; private boolean firstminus = true; public void onpagescrollstatechanged(int state) {} public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { if(mpagelastscreen!=position) { if (thresholdoffset > positionoffset) { toaster.toast("page has been scrolled left " + currentpage); if (firstplus){ firstplus = false; } else { currentpage++; firstminus = true; } mydatacontainer.currentview = currentpage; toaster.toast("currentview " + mydatacontainer.currentview); } else { toaster.toast("page has been scrolled right, current page : " + currentpage ); if (firstminus){ firstminus = false; } else { currentpage--; firstplus = true; } mydatacontainer.currentview = currentpage; toaster.toast("currentview " + mydatacontainer.currentview); } mpagelastscreen=position; } } public void onpageselected(int position) {} }); } /** * simple pager adapter represents 5 screenslidepagefragment objects, in * sequence. */ public class viewpageradapter extends fragmentpageradapter { private list<fragment> fragments; /** * @param fm * @param fragments */ public viewpageradapter(fragmentmanager fm, list<fragment> fragments) { super(fm); this.fragments = fragments; } /* (non-javadoc) * @see android.support.v4.app.fragmentpageradapter#getitem(int) */ @override public fragment getitem(int position) { return this.fragments.get(position); } /* (non-javadoc) * @see android.support.v4.view.pageradapter#getcount() */ @override public int getcount() { return this.fragments.size() - 1; } @override public int getitemposition(object object) { toaster.toast("i want number : " + currentpage + " " + mydatacontainer.currentview); return mydatacontainer.currentview; } } private class screenslidepageradapter extends fragmentstatepageradapter { public screenslidepageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { return new screenslidepagefragment(); } @override public int getcount() { return num_pages; } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.activity_screen_slide, container, false); // inflate layout fragment return rootview; } @override public void onattach(activity activity) { super.onattach(activity); } @override public void ondetach() { super.ondetach(); } }
this code creates 3 fragments @ time , when swipe other one, delete 1 of 3 load before , add fragment has created.
below code :
public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { if(mpagelastscreen!=position) { if (thresholdoffset > positionoffset) { toaster.toast("page has been scrolled left " + currentpage); if (firstplus){ firstplus = false; } else { currentpage++; firstminus = true; } mydatacontainer.currentview = currentpage; toaster.toast("currentview " + mydatacontainer.currentview); } else { toaster.toast("page has been scrolled right, current page : " + currentpage ); if (firstminus){ firstminus = false; } else { currentpage--; firstplus = true; } mydatacontainer.currentview = currentpage; toaster.toast("currentview " + mydatacontainer.currentview); } mpagelastscreen=position; } }
this part of code set current fragment looking .
Comments
Post a Comment