根据DrawerLayout文档的说明,“避免在动画过程中执行昂贵的操作,例如布局,因为这会导致口吃”。
因此,我尝试在继续操作之前先等待抽屉关闭:
@Override public void onItemClick(AdapterView parent
, View view
, int position
, long id) {
// Close the drawer
mDrawerLayout.closeDrawer(mDrawerList);
ExecutorService es = Executors.newSingleThreadExecutor();
final int mPosition = position;
Thread navThread = new Thread(new Runnable() {
@Override public void run() {
// Wait for the drawer to close
while (mDrawerLayout.isDrawerOpen(mDrawerList));
// Initialize the Fragments.
runOnUiThread(new Runnable() {
@Override public void run() {
selectNavigationItem(mPosition);
}
});
}
});
es.submit(navThread);
es.shutdown();
}
即使有效,Lint也会警告我有关空的while循环的信息。没有空的while循环,如何实现此效果?
最佳答案
在ListView.OnItemClickListener
中:
@Override public void onItemClick(AdapterView parent
, View view
, int position
, long id) {
mPosition = position;
mNavigationItemClicked = true;
mDrawerLayout.closeDrawer(mDrawerList);
}
在
DrawerLayout
中:public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
if (mNavigationItemClicked)
selectNavigationItem();
mNavigationItemClicked = false;
}
在
selectNavigationItem()
中:switch (mPosition) {
...
}