1

我有一个Navigation Drawer有 5 个项目。如果用户选择一个项目,我现在正在加载相应的项目FragmentFrameLayout 如果用户拉出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因此,如果我再次从Fragment负载中选择 Item no:3 。这个怎么解决??

编辑:

public int getPrevSelectedItem() {
    return selectedItem;
}

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

4 回答 4

1

尝试这个

Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.dashboard_container);
if(currentFragment instanceof YouFragmentName)
    return
于 2015-12-15T09:09:34.097 回答
1

您可以在单击菜单时检查加载片段的实例

Fragment loadedFragment= getFragmentManager().findFragmentById(R.id.fragmentLoadingSpace);

  if( !(loadedFragment instanceof  FragmentName1)){

// load fragment

        }
于 2015-12-15T09:34:25.937 回答
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);

}
于 2015-12-15T09:18:41.250 回答
-1

从我的评论:

drawer按下时不会更新-因此back您需要在其中添加一些代码onBackPressed(),根据弹出的activity更新。positionBackstackEntry

@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();
}
于 2015-12-15T11:17:26.917 回答