2013-04-18 123 views
0

我不能为起始页设定值(位置0)Viewpager位置0与使用onPageSelected(0)

我的意思是QuestionPagerAdapter.tv.setText( “默认”)必须MainActivity的使用onPageSelected工作(INT位置不工作)情况0:但它给零点。但它工作位置> 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; 

    } 

回答

0

既然你已经传递到的AsyncTask TextView的情况下,你可以使用onPreExecute()方法来设置默认任务开始执行之前的值。 onPreExecute()方法在UI线程上运行,所以不应该是一个问题。

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

+0

感谢您的回复,我已经改变了代码;删除asynctask。只想为起始页面(位置0)设置一个值,但它会给出零点 – metemet06 2013-04-18 02:24:56

0

视图传呼机大小必须> = 2,所以请检查您的适配器有大小> 0,否则,那么,

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