3 回答
TA贡献1797条经验 获得超6个赞
步骤一:添加 Nav Drawer 的监听器,在打开的回调中实现这个
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
将此代码实现到此方法中
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(navDrawer.isOpened()){
navDrawer.closeDrawers();
}
}
},2000);
TA贡献1847条经验 获得超11个赞
尝试在 2 秒后关闭
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
navDrawer.closeDrawers();
}
},2000);
TA贡献1943条经验 获得超7个赞
不要只是创建随机处理程序对象,因为这可能会导致诸如由于待处理指令(可运行对象)而随机关闭抽屉之类的问题。您可以直接将可运行对象发布/延迟后到视图。例如:-
class ExampleActivity extends AppCompatActivity {
private static final long AUTO_CLOSE_DELAY = 2000L;
@Nullable
private DrawerLayout drawerLayout;
@NonNull
private final Runnable closeDrawerRunnable = new Runnable() {
@Override
public void run() {
if(drawerLayout!=null && drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
drawerLayout = findViewById(R.id.drawerLayout);
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(@NonNull View drawerView) {
drawerLayout.removeCallbacks(closeDrawerRunnable);
drawerLayout.postDelayed(closeDrawerRunnable,AUTO_CLOSE_DELAY);
}
@Override
public void onDrawerClosed(@NonNull View drawerView) {
drawerLayout.removeCallbacks(closeDrawerRunnable);
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
}
}
添加回答
举报