避免在我的 android 可滑动标签中第一次尝试加载第 5 个标签

Avoid loading the 5th tab in 1st attempt in my android swipeable tabs

我在可切换选项卡视图中有一个片段,我想在所需的片段中显示我的电子邮件地址(它在第 5 个选项卡中)。当我使用此代码时,它会立即加载第 5 个屏幕。我该如何改变它。 我想先加载第一个选项卡作为普通选项卡。

我用了这个答案,Intent within fragment works only half the time

public class ActivityMenu 扩展了 FragmentActivity 实现 ActionBar.TabListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
-----tag-----------------------------------------------------   
    Fragment fragment = new ProfileFragment();
    Bundle bundle = new Bundle();

    // Put variables in bundle and add to fragment
    bundle.putString("user", getIntent().getStringExtra("user"));
    fragment.setArguments(bundle);

    // Insert the fragment
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager
            .beginTransaction()
            .add(android.R.id.content, fragment)
            .commit();
---------------------------------------------------------------------

//      String email = getIntent().getStringExtra("user");

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setDisplayUseLogoEnabled(true);

    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    actionBar.addTab(actionBar.newTab().setTabListener(this)
            .setIcon(R.drawable.ic_tab_p));
    actionBar.addTab(actionBar.newTab().setTabListener(this)
            .setIcon(R.drawable.ic_tab_n));
    actionBar.addTab(actionBar.newTab().setTabListener(this)
            .setIcon(R.drawable.ic_tab_s));
    actionBar.addTab(actionBar.newTab().setTabListener(this)
            .setIcon(R.drawable.ic_tab_p));
    actionBar.addTab(actionBar.newTab().setTabListener(this)
            .setIcon(R.drawable.ic_tab_p));
    ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    // set background for action bar tab
    bar.show();

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

适配器

public class TabsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            // Top Rated fragment activity
            return new PFragment();
        case 1:
            // Games fragment activity
            return new PiFragment();
        case 2:
            // Movies fragment activity
            return new SFragment();
        case 3:
            // Movies fragment activity
            return new PFragment();
        case 4:
            // Movies fragment activity
            return new PrFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 5;
    }

}

我想我得到这个是因为我正在创建新的捆绑包,但我该如何克服它,我们将不胜感激任何帮助。

在activity中,在onCreate()中获取emailStr,并删除所有其他虚线,带有额外参数的实例适配器emailStr:

String emailStr = getIntent().getStringExtra("user");

mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), emailStr);

在适配器中,相应地更改构造函数,并为第 5 个选项卡片段设置参数:

private String user = null;

public TabsPagerAdapter(FragmentManager fm, String user) {
    super(fm);

    this.user = user;
}

... ...

//add those codes to 5th tab fragment
Fragment fragment = new ProfileFragment(); 
Bundle bundle = new Bundle(); 
bundle.putString("email", user); 
fragment.setArguments(bundle);
return fragment;

希望这会奏效!