如何在viewpager中显示片段?

时间:2013-09-10 13:56:19

标签: java android xml android-fragments android-viewpager

我的应用目前包含3个片段,其中2个片段显示为actionbarsherlock libary上的标签。 每当我尝试用我的第三个片段替换我的第一个片段时,新视图都是空白的。

如何显示第3个?在调用方法getItem()?

时,是否需要返回一个新的实例化片段?

这是我的应用布局的illustration

public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener  {
ViewPager mViewPager;
myPageAdapter mPageAdapter;
List<Fragment> fragments;
ActionBar actionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    fragments = new Vector<Fragment>();
    fragments.add(LoginFragment.newInstance());
    fragments.add(EventFragment.newInstance());

    mPageAdapter = new myPageAdapter(getSupportFragmentManager(), fragments, this);

    actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setLogo(R.drawable.logo);
    actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mPageAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
            invalidateOptionsMenu();
        }
    });

    for (int i = 0; i < mPageAdapter.getCount(); i++) {
    actionBar.addTab(actionBar.newTab().setText(mPageAdapter.getPageTitle(i)).setTabListener(this));
    }

}


@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.findItem(R.id.itemRefresh).setVisible(mViewPager.getCurrentItem() == 1);
    menu.findItem(R.id.itemArchive).setVisible(mViewPager.getCurrentItem() == 0);
    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.layout.menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.itemRefresh:
        Toast.makeText(this, "updating page", Toast.LENGTH_SHORT).show();
        return true;
    case R.id.itemArchive:
        displayArchive();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

public void displayArchive() {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.pager, ArchiveFragment.newInstance());
    ft.addToBackStack(null);
    ft.commit();
}

Pageadapter

public class myPageAdapter extends FragmentPagerAdapter {
private Context context;
private List<Fragment> fragments;

public myPageAdapter(FragmentManager fm, List<Fragment> fragments, Context context) {
    super(fm);
    this.context = context;
    this.fragments = fragments;
}

@Override
public Fragment getItem(int position) {
    return fragments.get(position);
}

@Override
public int getCount() {
    return fragments.size();
}

@Override
public String getPageTitle(int position) {
    switch (position) {
    case 0:
        return context.getString(R.string.title_fragment1);
    case 1:
        return context.getString(R.string.title_fragment2);
    default:
        return null;
    }
}

}

主要活动xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>

提前致谢。

1 个答案:

答案 0 :(得分:0)

我发现不是将它们存储在arraylist中,而是在它们即将被检索时实例化它们。

当我想在两个片段之间切换时,我通过删除处理它,然后返回一个特定的片段,之后我通知数据集发生了变化。