我打算在打开导航抽屉时调用一些方法&关闭。我该怎么听呢(比如按钮的onclick听众)?
我知道我们可以检查(mDrawer.isDrawerOpen(Gravity.LEFT)
)或((mDrawer.isDrawerClose(Gravity.LEFT))
)。
先谢谢。
答案 0 :(得分:12)
是的,您可以使用四种抽象方法作为听众
public abstract void onDrawerClosed (View drawerView)
public abstract void onDrawerOpened (View drawerView)
public abstract void onDrawerSlide (View drawerView, float slideOffset)
public abstract void onDrawerStateChanged (int newState)
可以像这样使用
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
// Do whatever you want here
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
// Do whatever you want here
}
};
mDrawerLayout.addDrawerListener(mDrawerToggle);
从http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.DrawerListener.html
复制 UPDATE
API级别24.1.0中不推荐使用setDrawerListener
方法。所以使用addDrawerListener()
答案 1 :(得分:3)
您可以通过使用ActionBarDrawerToggle作为以下代码来了解它:
ActionBarDrawerToggle drawerToggle = new android.support.v7.app.ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
mDrawer.setDrawerListener(drawerToggle);
答案 2 :(得分:1)
'DrawerLayout'类的抽象内部类'DrawerListener'中提供了4种抽象方法
public abstract void onDrawerClosed (View drawerView)
public abstract void onDrawerOpened (View drawerView)
public abstract void onDrawerSlide (View drawerView, float slideOffset)
public abstract void onDrawerStateChanged (int newState)
有用的例子:
((DrawerLayout) findViewById(R.id.drawer_layout)).addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
// Whatever you want
}
@Override
public void onDrawerOpened(@NonNull View drawerView) {
// Whatever you want
}
@Override
public void onDrawerClosed(@NonNull View drawerView) {
// Whatever you want
}
@Override
public void onDrawerStateChanged(int newState) {
// Whatever you want
}
});
您可以查看此信息以获取更多信息HERE
答案 3 :(得分:1)
我知道这是一个很老的问题,但我也有同样的问题。
对于较新的 android 版本,您可以执行以下操作:
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(@NonNull View drawerView) {
}
@Override
public void onDrawerClosed(@NonNull View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
Toast.makeText(SideMenuActivity.this, "state: " + newState, Toast.LENGTH_SHORT).show();
}
});
答案 4 :(得分:0)
科特琳
初始化DrawerLayout视图
`val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)`
如果要在打开或关闭抽屉时自动执行操作,则可以执行以下操作。
在您的主要活动中,创建一个内部类,该内部类是DrawerLayout.DrawerListener的子类。 DrawerLayout类实现DrawerListener接口。如this post中所述,onDrawerSlide函数可用于在抽屉开始打开时进行操作。
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)
}
}
将操作添加到要使用的功能中。在我的示例中,如果用户与导航抽屉进行交互,则我将关闭软键盘。在您的主要活动中这样声明InputMethodManager:
var imm: InputMethodManager = this.getSystemService(Activity.INPUT_METHOD_SERVICE)
将您的自定义DrawerListener添加到抽屉布局(我将其放在onCreate方法中)
var drawerListener = CustomDrawer()
drawerLayout.addDrawerListener(drawerListener)