导航抽屉中的导航

时间:2015-12-15 09:03:32

标签: android android-fragments navigation navigation-drawer hamburger-menu

我有一个Navigation Drawer,有5个项目。 如果用户选择了一个项目,我会在Fragment中加载相应的FrameLayout 现在,如果用户拉出Navigation Drawer并选择与之前相同的项目,我不应再次加载相同的Fragment,所以我保存所选位置,如果先前选择的位置等于当前选择我我只是关闭Navigation Drawer,如下所示:

    String title = null;
    invalidateOptionsMenu();
    android.support.v4.app.Fragment fragment = null;
    if (position == getPrevSelectedItem()) {
        // Already selected fragment is being displayed
    } else {
        switch (position) {
            case Numerics.ZERO:
                fragment = new DashBoardFragment();
                title = fragment.getClass().getSimpleName();
                break;
            case Numerics.ONE:
                break;
            case Numerics.TWO:
                break;
            case Numerics.THREE:
                break;
            case Numerics.FOUR:
                break;
            default:
                break;
        }
        if (fragment != null) {
            showFragment(fragment, title);
        }
        setSelectedItem(position);
    }

我正在通过添加backstack添加片段,以便按以下方式提供对前一片段的反向导航:

    if (fragment != null) {
        String backStateName = fragment.getClass().getName();
        mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.replace(R.id.dashboard_container, fragment);
        mFragmentTransaction.addToBackStack(backStateName);
        mFragmentTransaction.commit();
    }

问题是:             如果我选择项目编号:3然后选择项目:4,如果我向后导航,前一个选择仍然是4,但我显示Fragment没有:3。因此,如果我再次从Navigation Drawer选择项目编号:3,则再次加载Fragment。怎么解决这个?

编辑:

public int getPrevSelectedItem() {
    return selectedItem;
}

public void setSelectedItem(int selectedItem) {
    this.selectedItem = selectedItem;
}

4 个答案:

答案 0 :(得分:1)

试试这个

Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.dashboard_container);
if(currentFragment instanceof YouFragmentName)
    return

答案 1 :(得分:1)

您可以在单击菜单

时检查已加载片段的实例
Fragment loadedFragment= getFragmentManager().findFragmentById(R.id.fragmentLoadingSpace);

  if( !(loadedFragment instanceof  FragmentName1)){

// load fragment

        }

答案 2 :(得分:0)

我认为不需要else声明,或者您只能else关闭drawer

public int prevPosition=-1;    

if (getPrevSelectedItem() != prevPosition) {
        // Already selected fragment is being displayed
        prevPosition =getPrevSelectedItem();  
        callFragment(position);
 } 
private void callFragment(int position) {
    // TODO Auto-generated method stub
    switch (position) {
    case Numerics.ZERO:
        fragment = new DashBoardFragment();
        title = fragment.getClass().getSimpleName();
        break;
    case Numerics.ONE:
        break;
    case Numerics.TWO:
        break;
    case Numerics.THREE:
        break;
    case Numerics.FOUR:
        break;
    default:
        break;
    }
    if (fragment != null) {
        showFragment(fragment, title);
    }
    setSelectedItem(position);

}

答案 3 :(得分:-1)

来自我的评论:

  

当您按drawer时,back无法更新 - 因此您需要在onBackPressed()中向activity添加一些代码,以便更新position根据弹出的BackstackEntry

@Override
public void onBackPressed(){

    String backStackEntryName = fragmentManager.getBackStackEntryAt(backCount - 2).getName(); 
     int id =  R.id.my_main_frag; //default option
     if (MyMainFrag.class.getName().equals(backStackEntryName)){
         id =  R.id.my_main_frag;
         selectedItem = 0;
     } else if (MySecondFrag.class.getName().equals(backStackEntryName)){
         id = R.id.my_second_frag;
         selectedItem = 1;
     }
      ...

     navigation.setCheckedItem(id);
     super.onBackPressed();
}