android - How can I use pager.setCurrentItem() inside a fragment? -
i created many fragments, fragment1 , fragment2.. , fragment50 inside first fragment listener method want call fragment40. didnt manage. inside listener these lines runs
pager.setcurrentitem(0); pager.setcurrentitem(pager.getcurrentitem() + 1); pager.setcurrentitem(pager.getcurrentitem() - 1); pager.setcurrentitem(40); not run. gives errors. please? //first fragment01 //i want call fragment40 public class frag_2 extends fragment implements onclicklistener, adapterview.onitemselectedlistener { //.................................................... public void onactivitycreated(bundle savedinstancestate) {super.onactivitycreated(savedinstancestate); final viewpager pager = (viewpager) getactivity().findviewbyid(r.id.pager); view txtdershakkinda1 = (view) getactivity().findviewbyid(r.id.txtdershakkinda1); txtdershakkinda1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { pager.setcurrentitem(pager.getcurrentitem()+40); // not run // pager.setcurrentitem(0); //runs // pager.setcurrentitem(pager.getcurrentitem() + 1); // pager.setcurrentitem(pager.getcurrentitem() - 1); } } ); //...................................................................... } /////////////////////// if many fragments --> fragmentstatepageradapter ///////////// public class mypageradapter extends fragmentstatepageradapter { private list<fragment> fragments; public mypageradapter(fragmentmanager fm) { super(fm); this.fragments = new arraylist<fragment>(); fragments.add(new frag_1<object>()); fragments.add(new frag_2()); fragments.add(new frag_3()); fragments.add(new frag_4()); ..................... fragments.add(new frag_50()); } @override public fragment getitem(int position) { return fragments.get(position); } @override public int getcount() { return fragments.size(); } }
add methods adapter :
@override public object instantiateitem(viewgroup container, int position) { fragment fragment = (fragment) super.instantiateitem(container, position); registeredfragments.put(position, fragment); return fragment; } @override public void destroyitem(viewgroup container, int position, object object) { registeredfragments.remove(position); super.destroyitem(container, position, object); } public fragment getregisteredfragment(int position) { return registeredfragments.get(position); }
and cant items this: adapater.getregisteredfragment(viewpager.getcurrentitem())
Comments
Post a Comment