Android 调用closeDrawers时导航抽屉未关闭-计时问题
单击导航抽屉列表中的项目时,我正在尝试关闭导航抽屉。我有2项活动A和B。以下代码与A和B的代码相同:Android 调用closeDrawers时导航抽屉未关闭-计时问题,android,android-intent,navigation-drawer,Android,Android Intent,Navigation Drawer,单击导航抽屉列表中的项目时,我正在尝试关闭导航抽屉。我有2项活动A和B。以下代码与A和B的代码相同: final DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); ListView mDrawerList = (ListView) findViewById(R.id.left_drawer); // Set the list's click listener mDrawerList.set
final DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ListView mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the list's click listener
mDrawerList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
Intent intent = null;
// first just close the drawer
mDrawerLayout.closeDrawers();
if (position == 0) { // Activity A
intent = new Intent(v.getContext(), ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
else if (position == 1) { // Activity B
intent = new Intent(v.getContext(), Activity B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
}
});
我发现:
- 如果我在活动中,并在抽屉中单击该活动的项目,抽屉将关闭
- 但是如果我去另一个活动,抽屉将不会关闭
closeDrawers()
之后,我尝试等待1秒,然后再执行其他操作(即更改活动):
//设置列表的单击侦听器
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(AdapterView arg0,最终视图v,最终整型位置,长arg3){
//先把抽屉关上
mDrawerLayout.closeDrawers();
最终处理程序=新处理程序();
handler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
意向=无效;
如果(位置==0){//活动A
intent=newintent(v.getContext(),ActivityA.class);
intent.setFlags(intent.FLAG\u ACTIVITY\u REORDER\u TO\u FRONT);
星触觉(意向);
}
如果(位置==1){//活动B
intent=newintent(v.getContext(),ActivityB.class);
intent.setFlags(intent.FLAG\u ACTIVITY\u REORDER\u TO\u FRONT);
星触觉(意向);
}
}
}, 1000);
}
});
果然,抽屉成功地关上了
我的问题是:
- 为什么会这样?我认为它们(关闭抽屉、更改活动)都是在同一个线程上按顺序运行的,所以一个线程必须在另一个线程执行之前完成
- 如何确保在执行任何其他操作之前关闭导航抽屉
开始活动
的执行速度将快于关闭导航抽屉
。关于第二个问题,您可以为此设置一个DrawerListener
,在导航完全关闭后,开始第二个活动:
mDrawerLayout.setDrawerListener(new DrawerListener() {
@Override
public void onDrawerStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onDrawerSlide(View arg0, float arg1) {
// TODO Auto-generated method stub
}
@Override
public void onDrawerOpened(View arg0) {
// TODO Auto-generated method stub
}
@Override
public void onDrawerClosed(View arg0) {
GotoActivity();
}
});
// Set the list's click listener
mDrawerList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, final View v, final int position, long arg3) {
// first just close the drawer
mDrawerLayout.closeDrawers();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = null;
if (position == 0) { // Activity A
intent = new Intent(v.getContext(), ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
else if (position == 1) { // Activity B
intent = new Intent(v.getContext(), ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
}
}, 1000);
}
});
mDrawerLayout.setDrawerListener(new DrawerListener() {
@Override
public void onDrawerStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onDrawerSlide(View arg0, float arg1) {
// TODO Auto-generated method stub
}
@Override
public void onDrawerOpened(View arg0) {
// TODO Auto-generated method stub
}
@Override
public void onDrawerClosed(View arg0) {
GotoActivity();
}
});