android - onPageSelected doesn't work for first page -
my pager adapter ( extends pageradepter ) has textview in it. , update textview mainactivity's onpageselected . update textview position > 0 , start page (position 0) doesn't update on startup. when swipe forward swipe back; sets value position 0 couldnt set value on start. how can set value star page ?
public void onpageselected(int position) { switch (position) { case 0: pageradapter.tv.settext("value"); // => doesnt work on start break; default: pageradapter.tv.settext("value"); break; } }
full code:
public class quizstart extends slidingfragmentactivity { @override protected void oncreate(final bundle savedinstancestate) { super.oncreate(savedinstancestate); this.setcontentview(r.layout.activity_main); mpager = (viewpager)findviewbyid(r.id.pager); questionpageradapter madapter = new questionpageradapter(); mpager.setadapter(madapter); quizstart.onpageselected(0); mpager.setonpagechangelistener(new onpagechangelistener() { @override public void onpagescrollstatechanged(int arg0) { } @override public void onpagescrolled(int arg0, float arg1, int arg2) { } @override public void onpageselected(int position) { switch (position) { case 0: pageradapter.tv.settext("value"); // => doesnt work on start break; default: pageradapter.tv.settext("value"); break; } } }); } }
you can manually call onpageselected(0)
when create onpagechangelistener
set starting state:
onpagechangelistener pagechangelistener = new onpagechangelistener() { @override public void onpagescrollstatechanged(int arg0) { } @override public void onpagescrolled(int arg0, float arg1, int arg2) { } @override public void onpageselected(int position) { switch (position) { case 0: quizstart.next.settext(getmycurrentpage()+""); break; default: quizstart.next.settext(getmycurrentpage()+""); break; } } }); mpager.setonpagechangelistener(pagechangelistener); // in runnable make sure viewpager's views instantiated before triggering onpageselected call mpager.post(new runnable() { @override public void run() { pagechangelistener .onpageselected(viewpager.getcurrentitem()); } });
edit
now not null pointer exception
pointed @metemet06
Comments
Post a Comment