3 回答

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
步驟一:添加 Nav Drawer 的監(jiān)聽器,在打開的回調(diào)中實(shí)現(xiàn)這個(gè)
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
將此代碼實(shí)現(xiàn)到此方法中
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(navDrawer.isOpened()){
navDrawer.closeDrawers();
}
}
},2000);

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊
嘗試在 2 秒后關(guān)閉
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
navDrawer.closeDrawers();
}
},2000);

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
不要只是創(chuàng)建隨機(jī)處理程序?qū)ο?,因?yàn)檫@可能會(huì)導(dǎo)致諸如由于待處理指令(可運(yùn)行對象)而隨機(jī)關(guān)閉抽屜之類的問題。您可以直接將可運(yùn)行對象發(fā)布/延遲后到視圖。例如:-
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) {
}
});
}
}
添加回答
舉報(bào)