android - need help to fix code for getting current view in viewpager -
need fix code saw in https://stackoverflow.com/a/9275732/1938357. solution need current view onany page in viewpager based application
below code reference
public view getcurrentview(viewpager pager) { (int = 0; < pager.getchildcount(); i++) { view child = pager.getchildat(i); if (child.getx() <= pager.getscrollx() + pager.getwidth() && child.getx() + child.getwidth() >= pager.getscrollx() + pager.getwidth()) { return child; } } return getchildat(0);
i trying access current view in view pager , used code above. works fine when scroll forwards. when scroll backwards after 2 backward scrolls stops working.
public class myactivity extends fragmentactivity //variable declarations protected void oncreate(bundle savedinstancestate) { viewpager mpager = (viewpager) findviewbyid(r.id.pager); mpageradapter = new screenslidepageradapter(getsupportfragmentmanager()); mpager.setadapter(mpageradapter); view currview; onpagechangelistener pagechangelistener = new onpagechangelistener() { @override public void onpageselected(int pageselected) { dotextviewchnges();//access text view , update based on pageselected ---this stuck in trying textview in current fragmwnt/view------- } mpager.setonpagechangelistener(pagechangelistener); } private class screenslidepageradapter extends fragmentstatepageradapter { public screenslidepageradapter(android.support.v4.app.fragmentmanager fm) { super(fm); } @override public android.support.v4.app.fragment getitem(int position) { return screenslidepagefragment.create(position, <other parameters want pass>); } @override public int getcount() { return num_pages; } }
and below screenslidepagefragment class
public class screenslidepagefragment extends android.support.v4.app.fragment { public static final string arg_page = "pagenumber"; public static final string arg_pagearray = "pages"; private int mpagenumber; //declare other variables @suppresslint("newapi") public static screenslidepagefragment create(int pagenumber, string[] pages, int bookmarkpagenumber, string storyname, int linesinonepage) { screenslidepagefragment fragment = new screenslidepagefragment(); bundle args = new bundle(); args.putint(arg_page, pagenumber); args.putstringarray(arg_pagearray, pages); fragment.setarguments(args); return fragment; } @suppresslint("newapi") public screenslidepagefragment() { } @suppresslint("newapi") @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mpagenumber = getarguments().getint(arg_page); pages = getarguments().getstringarray(arg_pagearray); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { rootview = (viewgroup) inflater .inflate(r.layout.activity_story, container, false); //load layout components filled page loadview(rootview); return rootview; } public int getpagenumber() { return mpagenumber; }
it hard discuss in comments example of code.
as see in grepcode fragmentstatepageradapter.setprimaryitem(), receive object
android.support.v4.app.fragment
. receive classcastexception
when trying cast object fragment
. can check casting: cast object in android.support.v4.app.fragment
not in android.app.fragment
? if right, add exception message question, please.
update:
if screenslidepageradapter
has method getcurrentview(), method 'getcurrentview()' can work this:
public view getcurrentview(viewpager pager) { return ((screenslidepageradapter) pager.getadapter()).getcurrentview(); }
and method getcurrentview()
of screenslidepageradapter
:
public view getcurrentview() { return currview; }
update 2:
i've made modification code:
your activity private adapter:
public class myactivity extends fragmentactivity implements iselectitemlistener { // variable declarations , methods @override protected void oncreate(bundle savedinstancestate) { final viewpager pager = (viewpager) findviewbyid(r.id.pager); mpageradapter = new screenslidepageradapter(getsupportfragmentmanager()); pager.setadapter(mpageradapter); pager.setonpagechangelistener( new onpagechangelistener() { @override public void onpageselected(int pageselected) { final screenslidepagefragment fragment = mpageradapter.getfragment(pageselected); if (fragment != null) { fragment.dotextviewchnges(); } } @override public void onpagescrollstatechanged(int arg0) { // todo auto-generated method stub } @override public void onpagescrolled(int arg0, float arg1, int arg2) { // todo auto-generated method stub } }); } private class screenslidepageradapter extends fragmentstatepageradapter { //your variable declarations private final screenslidepagefragment[] mfragments; public screenslidepageradapter(android.support.v4.app.fragmentmanager fm) { super(fm); mfragments = new screenslidepagefragment[num_pages]; } @override public int getcount() { return num_pages; } // create new screenslidepagefragment position if it's not exsist @override public android.support.v4.app.fragment getitem(int position) { if (getfragment(position) == null) { mfragments[position] = screenslidepagefragment.create(position, <other parameters want pass>); } return getfragment(position); } // give screenslidepagefragment if it's exists, or null public screenslidepagefragment getfragment(int position) { return mfragments[position]; } } }
your fragment:
public class screenslidepagefragment extends fragment { //your variables , methods public static screenslidepagefragment create(int pagenumber, string[] pages, int bookmarkpagenumber, string storyname, int linesinonepage) { final bundle args = new bundle(); args.putint(arg_page, pagenumber); args.putstringarray(arg_pagearray, pages); final screenslidepagefragment fragment = new screenslidepagefragment(); fragment.setarguments(args); return fragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mpagenumber = getarguments().getint(arg_page); pages = getarguments().getstringarray(arg_pagearray); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { rootview = (viewgroup) inflater.inflate(r.layout.activity_story, container, false); loadview(rootview); return rootview; } public int getpagenumber() { return mpagenumber; } // let's keep logic of modifications in fragment public void dotextviewchnges() { final view view = getview(); // want view , children } }
Comments
Post a Comment