Viewpager位置0不能与onPageSelected(0)一起工作



我不能设置起始页(位置0)的值

我的意思是QuestionPagerAdapter.tv.setText("default")必须在MainActivity的onPageSelected(int position)情况下工作0:但它给出了空点。但是对于position> 0

PagerAdapter:

public class QuestionPagerAdapter extends PagerAdapter{
    public static TextView tv;
@Override
    public Object instantiateItem(View collection, int pos) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View page = inflater.inflate(R.layout.page_quiz, null);
         tv = (TextView)page.findViewById(R.id.questionText);

}
}

MainActivity:

    @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);
@Override
            public void onPageSelected(int position) {
                switch (position) {
            case 0: 
     //       QuestionPagerAdapter.tv.setText("default");  => doesnt works
                break;
                  default:
        QuestionPagerAdapter.tv.setText("default");  =>  works
                               break;
    }

既然你有textView实例传递给AsyncTask,你可以使用onPreExecute()方法在任务开始执行之前设置默认值。onPreExecute()方法在UI线程上运行,所以不应该是一个问题。

http://developer.android.com/reference/android/os/AsyncTask.html onPreExecute ()

视图页必须有size>=2,所以检查你的适配器是否有size>0,否则,

@Override
public int getCount() {
    return 2;// a view pager has minimum 2 pages
}

最新更新