这是我的 TabListener:
public class TabListener<T extends Fragment> implements android.support.v7.app.ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabListener(Activity activity, String tag, Class<T> cl) {
mActivity = activity;
mTag = tag;
mClass = cl;
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mFragment = ((FragmentActivity) mActivity).getSupportFragmentManager().findFragmentByTag(mTag); // add this
if (mFragment == null){
mFragment = android.support.v4.app.Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content,mFragment,mTag);
} else {
ft.attach(mFragment);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
mFragment = ((FragmentActivity) mActivity).getSupportFragmentManager().findFragmentByTag(mTag); // add this
if (mFragment != null) {
//Toast.makeText(mActivity, "detached", Toast.LENGTH_SHORT).show();
ft.detach(mFragment);
}
}
}
我的活动的一部分:
public class RoadMapActivity extends ActionBarActivity {
ListView generalRoadmapList;
ListView mainRoadmapList;
android.support.v7.app.ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roadmap);
actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab generalTab = actionBar.newTab()
.setText("General")
.setTabListener(new TabListener<GeneralListFragment>(this, "general", GeneralListFragment.class));
actionBar.addTab(generalTab);
Tab myTab = actionBar.newTab()
.setText("My")
.setTabListener(new TabListener<MyListFragment>(this, "my", MyListFragment.class));
actionBar.addTab(myTab);
}
我试图将 viewGroup 从 android.R.id.content 更改为 Activity 布局中的自定义视图组,接下来会发生:
我有两个选项卡,A 和 B。启动活动后,活动选项卡是 A,内容是片段 A。如果切换到 B,选项卡 B 会激活,但内容仍然是片段 A。然后改回选项卡 A ,内容将是片段 B。更改为选项卡 B,内容是片段 A... 不显示后面的另一个片段,但更改了内容。
我已经尝试了我在谷歌和这里找到的所有东西,但没有任何成功。有人有想法吗?