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

khbbv19g  于 2022-12-21  发布在  Android
关注(0)|答案(8)|浏览(310)

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

gmol1639

gmol16391#

假设您已经在xml中定义了一个drawerlayout:

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

0sgqnhkj2#

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

听众用途:)

z8dt9xmd

z8dt9xmd3#

use:

    • mDrawerLayout. isDrawerOpen()**方法

示例:

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

unguejic4#

通过下面的方法你可以发现抽屉是打开还是关闭的。

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
kkbh8khc

kkbh8khc5#

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

rslzwgfq6#

也许,我迟到了,但我可以分享新的更新信息。在给定的代码中,我在onclick函数中设置了打开/关闭抽屉。如果抽屉是打开的,那么它将关闭,否则打开。这个解决方案是基于RXava DrawerLayout (androidx.drawerlayout.widget.DrawerLayout)的。在我的代码中,我从右到左打开drawerLayout。

@Override
    public void onClick(View view) {

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

}

注:如果您从右侧打开drawerLayout,请使用**“GravityCompat.End”,它从左侧打开,然后使用“GravityCompat.Start”**。享受编码.....

ftf50wuq

ftf50wuq7#

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

ijnw1ujt8#

Kotlin的方法是相同的。
1.初始化DrawerLayout视图
第一个月
1.检查抽屉是否打开
if(drawerLayout.isDrawerOpen(GravityCompat.START)){ Log.d("Drawer","open") }
如果要在抽屉打开或关闭时自动执行操作,可以执行以下操作。
1.在主Activity中,创建一个内部类,该内部类是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)
  }
}

1.把你的操作放在你想使用的函数中,在我的例子中,如果用户与抽屉式导航交互,我将关闭软键盘。在你的主Activity中声明InputMethodManager,如下所示:
var imm: InputMethodManager = this.getSystemService(Activity.INPUT_METHOD_SERVICE)
1.将您的自定义DrawerListener添加到drawerLayout中(我将它放在onCreate方法中)
x1米3英寸x1米4英寸

相关问题