我的页面适配器(扩展PagerAdepter)中有一个textview。我使用MainActivity的onPageSelected更新这个textview。它会为位置大于0的页面更新textview,但是起始页面(位置0)在启动时不会更新。当我向前滑动再向后滑动时,它会设置位置0的值,但我无法在启动时设置值。如何设置起始页面的值?
public void onPageSelected(int position) {
switch (position) {
case 0:
PagerAdapter.tv.setText("value"); // => doesnt work on start
break;
default:
PagerAdapter.tv.setText("value");
break;
}
}
完整的代码:
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;
}
}
});
}
}