1

嗨,我正在尝试开发一个操作栏导航屏幕,其中显示两个选项卡。我正在激活 onTabselected 侦听器中的片段。但是直到我调用 fagementtransaction.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);
        }
    }

}
}
4

1 回答 1

0

我建议使用 Activity 而不是 FragmentActivity。希望它有效

于 2013-03-14T06:13:25.523 回答