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


89

标题说明了一切。我要做的就是知道导航抽屉是否打开。我在网上进行了很多搜索,找到了该方法,isDrawerOpen(int drawerGravity)但找不到令人满意的答案来说明如何在方法中使用它。如果有人向我解释,我将不胜感激。

提前致谢!


3
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList)。mDrawerLayout是抽屉,mDrawerList是listview
Raghunandan 2014年

@Raghunandan非常感谢!
Chinmay Dabke 2014年

Answers:


213

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

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

这是否意味着抽屉是打开的?
Chinmay Dabke 2014年

8
虽然对许多人来说可能很明显,但我想指出的是,如果从右侧打开抽屉,则应使用isDrawerOpen(GravityCompat.END))(即android:layout_gravity =“ right”)

3
如果要关闭,则应添加此注释,如果注释staments.closeDrawer(Gravity.START);
穆罕默德(Mohmet)'17

32
 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);

该听众使用:)


谢谢,但我没有在导航抽屉中使用操作栏。只需要在一种方法中使用它!
Chinmay Dabke 2014年

20

用:

mDrawerLayout.isDrawerOpen()方法

例:

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

3
如果抽屉是打开的,则打开抽屉,否则关闭抽屉:D
ataulm

5

通过以下方式,您可以发现抽屉处于打开或关闭状态。

 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

由于set已过时,请使用addDrawerListener
SeaRoth

3

shakeJ的答案是正确的,请记住您可以使用onDrawerSlide来触发其他功能……例如,我使用它来更改StatusBar颜色。

        /** 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");
            }
        }

3

检测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

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添加到抽屉布局(我将其放在onCreate方法中)

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


1

可能是,我迟到了,但是我可以分享新的更新信息。在给定的代码中,我在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”。享受编码。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.