0

我的应用程序当前包含 3 个片段,其中 2 个在 actionbarsherlock 库上显示为选项卡。每当我尝试用我的第三个片段替换我的第一个片段时,新视图都是空白的。

如何显示第三个?调用getItem()方法时是否需要返回一个新的实例化片段?

这是我的应用程序布局的插图

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();
}

页面适配器

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>

提前致谢。

4

1 回答 1

0

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

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

于 2013-09-18T10:56:51.087 回答