我已经实现了导航抽屉,我想在导航抽屉关闭之前加载我的片段。目前,片段与抽屉关闭并行加载,因此如果片段很重,则用户界面会挂起一点。
我的代码是:
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragmentProfile);
ft.commit();
drawerLayout.closeDrawer(drawerNaviListView);
}
}
如何更改此设置以便我首先看到我的片段加载(在后台),当它完成加载时,导航抽屉会关闭?
答案 0 :(得分:3)
我的解决方案是在抽屉关闭后加载片段: 实际上在onDrawerClosed中调用loadFragment方法
public void onDrawerClosed() {
// assure the request comes from selecting a menu item, not just closing tab
if (selectedTab )
selectItem(mSelectedFragment);
selectedTab = false;
}
答案 1 :(得分:3)
在抽屉关闭后尝试装入 使用handler来创建延迟执行。因此,当抽屉关闭时,不会有任何挂起
在方法onItemClick()
内,使用以下代码:
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragmentProfile);
ft.commit();
}
};
handler.postDelayed(runnable, 500); // here 500 is the delay
//其他方式是
创建一个AsyncTask并在doInBackground()方法中启动片段事务并关闭onPostExecute()中的抽屉
答案 2 :(得分:2)
DrawerLayout.DrawerListener可用于监视抽屉视图的状态和动作。 避免执行昂贵的操作,例如动画期间的布局,因为它可能会导致 口吃;尝试在STATE_IDLE状态期间执行昂贵的操作。 Source
换句话说,Android建议您在交换碎片之前等待抽屉关闭。