【问题标题】:Closing drawer when navigating to current activity导航到当前活动时关闭抽屉
【发布时间】:2014-06-17 00:44:31
【问题描述】:

我正在使用导航抽屉,但遇到了一个小问题。当我在我的一个活动中,并通过导航抽屉导航到同一个活动时,就会出现问题。活动启动新的(屏幕弹出)。

我希望抽屉在用户尝试导航到他们当前正在进行的活动时关闭。

感谢您的帮助!

【问题讨论】:

    标签: android android-layout android-activity


    【解决方案1】:

    为什么不只检查当前的Activity,伪代码:

    if (this instanceof SomeActivity){
        closedrawer();
    }else{
        navigate to SomeActivity
    }
    

    【讨论】:

      【解决方案2】:
      //global reference
      private DrawerLayout mDrawerLayout;
      
      //instantiate onCreate()
      mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout);
      
      //close it some where, on button click...
      if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
          mDrawerLayout.closeDrawer(Gravity.LEFT);
      }
      

      【讨论】:

        【解决方案3】:

        在您处理来自导航抽屉的点击的方法中:

        // This is for the case where the user is trying to open a fragment called `NewFragment`
        Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(NewFragment.TAG);
        if (currentFragment == null) {
           NewFragment newFragment = new NewFragment()
           FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
           transaction.replace(R.id.fragment_container, newFragment, NewFragment.TAG);
           transaction.commit();
        }
        mDrawerLayout.closeDrawer(mDrawerList);
        

        如果您已将片段添加到带有标记的片段容器中,则此方法有效。我通常将其作为public static final String 保留在相应的片段中。例如,NewFragment 有一个名为 TAG 的静态 String,可用于稍后使用片段管理器找到它。

        如果currentFragment 为空,则表示当前未添加到管理器的活动中,也未添加到后台堆栈中。在这种情况下,将新片段添加到片段容器中。如果没有,只需关闭抽屉式导航即可。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多