如何检测导航抽屉是否打开?

问题描述 投票:0回答:9

标题已经说明了一切。我想做的就是知道导航抽屉是否打开。我在网上搜索了很多,找到了方法

isDrawerOpen(int drawerGravity)
,但找不到令人满意的答案来解释如何在方法中使用它。如果有人向我解释,我将不胜感激。

android android-navigation
9个回答
221
投票

假设您已经在 xml 中定义了抽屉布局:

DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
...
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
  //drawer is open
}

36
投票
 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description */
            R.string.drawer_close  /* "close drawer" description */
            ) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getActionBar().setTitle(mTitle);
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getActionBar().setTitle(mDrawerTitle);
        }
    };

    // Set the drawer toggle as the DrawerListener
    mDrawerLayout.setDrawerListener(mDrawerToggle);

听众使用:)


20
投票

用途:

mDrawerLayout.isDrawerOpen()方法

示例:

    if(mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
        mDrawerLayout.closeDrawer(Gravity.LEFT); //CLOSE Nav Drawer!
    }else{
        mDrawerLayout.openDrawer(Gravity.LEFT); //OPEN Nav Drawer!
    }

6
投票

通过以下方式可以发现抽屉是打开还是关闭..

 public class YourActivity extends AppCompatActivity implements  DrawerLayout.DrawerListener {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dash_board);

        DrawerLayout drawer=(DrawerLayout)findViewById(R.id.container);
        drawer.setDrawerListener(this);
    }//onCreate()

    @Override
    public void onDrawerOpened(View arg0) {
      //write your code
    }

    @Override
    public void onDrawerClosed(View arg0) {
        //write your code
    }

    @Override
    public void onDrawerSlide(View arg0, float arg1) {
        //write your code
    }

    @Override
    public void onDrawerStateChanged(int arg0) {
        //write your code
    }
}//class

4
投票

检测

DrawerLayout
打开、关闭、滑动DrawerLayout.DrawerListener

DrawerLayout drawerLayout:
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
    /**
     * Called when a drawer's position changes.
     *
     * @param slideOffset The new offset of this drawer within its range, from 0-1
     *                    Example when you slide drawer from left to right, slideOffset will increase from 0 - 1 (0 when drawer closed and 1 when drawer display full)
     */
    @Override
    public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

    }

    @Override
    public void onDrawerOpened(@NonNull View drawerView) {
        // do something when drawer opened
    }

    @Override
    public void onDrawerClosed(@NonNull View drawerView) {
        // do something when drawer closed
    }

    /**
     * Called when the drawer motion state changes. The new state will
     * be one of {@link #STATE_IDLE}, {@link #STATE_DRAGGING} or {@link #STATE_SETTLING}.
     */
    @Override
    public void onDrawerStateChanged(int newState) {

    }
});

检查抽屉是否打开

if(drawerLayout.isDrawerOpen(GravityCompat.START)) // or  GravityCompat.END

3
投票

也许,我迟到了,但我可以分享新的更新信息。 在给定的代码中,我在 onclick 函数中设置打开/关闭抽屉。如果抽屉打开,则它将关闭,否则打开。此解决方案基于 RXava DrawerLayout

(androidx.drawerlayout.widget.DrawerLayout)
。 在我的代码中,我从右到左打开抽屉布局。

 @Override
    public void onClick(View view) {

  if (drawerLayout.isDrawerVisible(GravityCompat.END)) {
                    drawerLayout.closeDrawer(GravityCompat.END);
  }else {
                    drawerLayout.openDrawer(GravityCompat.END);
  }

}

注意:如果您从右侧打开抽屉布局,请使用“GravityCompat.END”,并且从左侧打开抽屉布局,则使用“GravityCompat.START”。 享受编码......


2
投票

shakeJ 的答案是正确的,请记住您可以使用 onDrawerSlide 来启动其他功能...例如我用它来更改状态栏颜色。

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            isOpen = false;
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            isOpen = true;
        }
        public void onDrawerSlide(View drawerView,float slideOffset){
            super.onDrawerSlide(drawerView,slideOffset);
            if(!isOpen){
                setStatusBarColor("#00102b");
            }
            if(isOpen){
                setStatusBarColor("#EFEFF0");
            }
        }

1
投票

Kotlin 中的方法是一样的。

  1. 初始化DrawerLayout视图

    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)

  2. 检查抽屉是否打开

    if(drawerLayout.isDrawerOpen(GravityCompat.START)){
            Log.d("Drawer","open")
     }

如果您想在抽屉打开或关闭时自动执行操作,可以执行以下操作。

  1. 在您的主要活动中,创建一个内部类,它是 DrawerLayout.DrawerListener 的子类。 DrawerLayout 类实现 DrawerListener 接口。

    inner class CustomDrawer : DrawerLayout.DrawerListener{
      override fun onDrawerStateChanged(newState: Int) {
      }
    
      override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
        imm.hideSoftInputFromWindow(drawerView?.getWindowToken(), 0)
      }
    
      override fun onDrawerClosed(drawerView: View) {
        imm.hideSoftInputFromWindow(drawerView?.getWindowToken(), 0)
      }
    
      override fun onDrawerOpened(drawerView: View) {
        imm.hideSoftInputFromWindow(drawerView?.getWindowToken(), 0)
      }
    }
    
  2. 将您的操作放入您要使用的功能中。在我的示例中,如果用户与导航抽屉交互,我将关闭软键盘。在您的主要活动中像这样声明 InputMethodManager:

    var imm: InputMethodManager = this.getSystemService(Activity.INPUT_METHOD_SERVICE)

  3. 将自定义的DrawerListener添加到drawerLayout中(我把它放在onCreate方法中)

    var drawerListener = CustomDrawer()
    drawerLayout.addDrawerListener(drawerListener)


0
投票

添加有关使用

DrawerLayout#isDrawerOpen
的其他答案:

我发现,如果我在

onStart
(或早期的生命周期回调!)中使用该方法来检测屏幕旋转(或类似的配置更改)后抽屉是否仍然打开,那么它会返回
false
,即使抽屉是开着的。从
onResume
调用此方法确实会返回
true
false
,用于在配置更改后分别打开和关闭的抽屉。

© www.soinside.com 2019 - 2024. All rights reserved.