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);
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
/** 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");
}
}
8条答案
按热度按时间gmol16391#
假设您已经在xml中定义了一个drawerlayout:
0sgqnhkj2#
听众用途:)
z8dt9xmd3#
use:
示例:
unguejic4#
通过下面的方法你可以发现抽屉是打开还是关闭的。
kkbh8khc5#
检测
DrawerLayout
打开、关闭、滑动DrawerLayout.DrawerListener支票抽屉打开
rslzwgfq6#
也许,我迟到了,但我可以分享新的更新信息。在给定的代码中,我在onclick函数中设置了打开/关闭抽屉。如果抽屉是打开的,那么它将关闭,否则打开。这个解决方案是基于RXava DrawerLayout
(androidx.drawerlayout.widget.DrawerLayout)
的。在我的代码中,我从右到左打开drawerLayout。注:如果您从右侧打开drawerLayout,请使用**“GravityCompat.End”,它从左侧打开,然后使用“GravityCompat.Start”**。享受编码.....
ftf50wuq7#
shakeJ的答案是正确的,请记住,您可以使用 onDrawerSlide 来激发其他函数...例如,我使用它来更改状态栏颜色。
ijnw1ujt8#
Kotlin的方法是相同的。
1.初始化DrawerLayout视图
第一个月
1.检查抽屉是否打开
if(drawerLayout.isDrawerOpen(GravityCompat.START)){ Log.d("Drawer","open") }
如果要在抽屉打开或关闭时自动执行操作,可以执行以下操作。
1.在主Activity中,创建一个内部类,该内部类是DrawerLayout.DrawerListener的子类。DrawerLayout类实现DrawerListener接口。
1.把你的操作放在你想使用的函数中,在我的例子中,如果用户与抽屉式导航交互,我将关闭软键盘。在你的主Activity中声明InputMethodManager,如下所示:
var imm: InputMethodManager = this.getSystemService(Activity.INPUT_METHOD_SERVICE)
1.将您的自定义DrawerListener添加到drawerLayout中(我将它放在onCreate方法中)
x1米3英寸x1米4英寸