我在一个可以发布的标签视图中有一个片段,我想在所需的片段中显示我的电子邮件地址(它在第5个标签中)。当我使用这个代码时,它直接加载第5个屏幕。我怎么能改变它。 我想先将第一个标签加载为普通标签。
我使用了Intent within fragment works only half the time
中的答案公共类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;
}
}
我认为我得到这个因为我正在创建新的捆绑,但我怎么能克服它,任何帮助将受到高度赞赏。
答案 0 :(得分:1)
在活动中,在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;
希望这会有效!