ActionBar列表导航重叠片段

时间:2012-05-10 19:40:18

标签: android android-fragments android-actionbar

我正在尝试在列表导航模式下实现Android操作栏,它在从列表中选择项目时成功更改了片段,但片段重叠,我可以看到前一个片段的内容仍在屏幕上第二个被选中。这是我的Activity的OnCreate和OnNavigationItemSelected的代码:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    mFirstFragment = new FirstFragment();
    mSecondFragment = new SecondFragment();

    SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(
            this, 
            R.array.action_list, 
            android.R.layout.simple_spinner_dropdown_item);

    mActionBar = getActionBar();
    mActionBar.setDisplayShowTitleEnabled(false);
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    mActionBar.setListNavigationCallbacks(mSpinnerAdapter, this);

    if(savedInstanceState != null) {
        mActionBar.setSelectedNavigationItem(
                savedInstanceState.getInt("currFragment"));
    }
}

public boolean onNavigationItemSelected(int position, long itemId) {
    FragmentTransaction mFragmentTransaction = 
            getFragmentManager().beginTransaction();

    switch(position) {
        case FIRST_FRAGMENT:
            mFragmentTransaction.replace(
                    android.R.id.content, 
                    mFirstFragment);
            break;
        case SECOND_FRAGMENT:
            mFragmentTransaction.replace(
                    android.R.id.content, 
                    mSecondFragment);
            break;
    }
    mFragmentTransaction.commit();

    return true;
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

我有同样的问题。 FragmentTransaction .attach and .detach for Actionbar tabs中接受的答案对我有用。你也可以从Android Action Bar Tab with scrollview made duplicate view after orientation change得到很好的指示(虽然对我有用的关键见解来自我所链接的第一个问题)。