如何在viewpager中使用instantiateItem()来更新页面

时间:2012-12-03 20:04:36

标签: android android-fragments android-viewpager

  

可能重复:
  how can i update view when fragment change?

有一个活动,其中包含2个片段前两个页面显示具有自定义列表视图的片段,这些视图是使用AsyncTask从服务器的xml构建的。但是,当应用程序运行时,只显示一个列表视图,另一个页面只是空白

我可以使用此方法更新此寻呼机

public Object instantiateItem(View container, int position) {

我的适配器代码是

public class VpiAbsTestActivity extends SherlockFragmentActivity {


private static final String[] CONTENT = new String[] { "1","2"};

TestFragmentAdapter mAdapter;
 ViewPager mPager;
 PageIndicator mIndicator;



protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_tabs);

mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);

mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
mIndicator.notifyDataSetChanged();
}



class TestFragmentAdapter extends FragmentPagerAdapter {        
private int mCount = CONTENT.length;

public TestFragmentAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    switch(position) {

    case 0:
        return new customlist();


    case 1:
        return new customlistnotuser();


    default:
        return null;

    }
}

@Override
public int getCount() {
    return mCount;
}


public CharSequence getPageTitle(int position) {
    return VpiAbsTestActivity.CONTENT[position %             VpiAbsTestActivity.CONTENT.length].toUpperCase();
}

 @Override
    public void destroyItem(View collection, int position, Object view) {
         ((ViewPager) collection).removeView((View) view);
    }

 }

0 个答案:

没有答案