您好我正在尝试开发一个操作栏导航屏幕,其中我显示两个tabs.I我在onTabselected侦听器中激活片段。但直到我调用fragementtransaction.commit()更改没有反映。它只工作一次,我的意思是,如果我切换到tab2并再次回到tab1,片段不会刷新。当切换到tab2时,tab2的内容是在tab1上绘制,看起来像重叠。我在这里发布我的代码,请帮我解决这个问题。
这是我的班级:
public class ContentActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle arg0) {
this.requestWindowFeature(Window.FEATURE_ACTION_BAR);
// this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
// WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//this.getWindow().setDimAmount((float) 30.0);
LayoutParams params = this.getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT; //fixed height
params.width = LayoutParams.MATCH_PARENT; //fixed width
this.getWindow().setGravity(Gravity.START);
this.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
super.onCreate(arg0);
setContentView(R.layout.layout_content);
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab BookMarkTab = actionbar.newTab().setText("BookMarks");
ActionBar.Tab NotesTab = actionbar.newTab().setText("Notes");
// Fragment bookMarkFragment = new BookMarkFragment();
//Fragment notesFragment = new NotesFragment();
BookMarkTab.setTabListener(new ContentTabListner(BookMarkFragment.class,"BookMarks"));
NotesTab.setTabListener(new ContentTabListner(NotesFragment.class,"Notes"));
actionbar.addTab(BookMarkTab);
actionbar.addTab(NotesTab);
}
class ContentTabListner implements ActionBar.TabListener {
private Fragment fragment;
private Class mclz;
private String mTag;
android.support.v4.app.FragmentTransaction fft = ContentActivity.this
.getSupportFragmentManager().beginTransaction();
public ContentTabListner(Class clazz,String tag) {
mclz = clazz;
mTag = tag;
fragment = ContentActivity.this.getSupportFragmentManager().findFragmentByTag(mTag);
if (fragment != null && !fragment.isDetached()) {
android.support.v4.app.FragmentTransaction ft = ContentActivity.this.getSupportFragmentManager().beginTransaction();
ft.detach(fragment);
ft.commit();
}
}
@Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
if(fragment == null){
fragment = Fragment.instantiate(ContentActivity.this, mclz.getName(),null);
fft.add(android.R.id.content,fragment, mTag);
fft.commit();
}else{
fft.attach(fragment);
}
}
@Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
if(fragment != null){
fft.detach(fragment);
}
}
}
}
答案 0 :(得分:0)
我建议使用Activity而不是FragmentActivity.Hope它可以工作