每次打开新活动时导航抽屉都会滞后。我在谷歌寻找解决方案,我发现我可以通过使用处理程序延迟新活动来解决它。我做了一点实验,但无处可去。
MainActivity.java中的一些代码:
public void SelectItem(int possition) {
Fragment fragment = null;
Bundle args = new Bundle();
switch (possition) {
case 2:
fragment = new FragmentZero();
break;
case 3:
fragment = new FragmentOne();
break;
case 4:
fragment = new FragmentTwo();
break;
case 5:
fragment = new FragmentThree();
break;
case 7:
fragment = new FragmentTwo();
break;
case 8:
fragment = new FragmentZero();
break;
case 9:
fragment = new FragmentOne();
break;
case 10:
fragment = new FragmentTwo();
break;
case 11:
fragment = new FragmentZero();
break;
case 12:
fragment = new FragmentOne();
break;
case 14:
fragment = new FragmentZero();
break;
case 15:
fragment = new FragmentOne();
break;
case 16:
fragment = new FragmentTwo();
break;
default:
break;
}
fragment.setArguments(args);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
mDrawerList.setItemChecked(possition, true);
setTitle(dataList.get(possition).getItemName());
mDrawerLayout.closeDrawer(mDrawerList);
}
和
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (dataList.get(position).getTitle() == null) {
SelectItem(position);
}
}
}
答案 0 :(得分:2)
在您的活动中创建一个处理程序并在您的on create方法
中初始化它private Handler mHandler;
mHandler = new Handler();
然后将您的抽屉项目单击侦听器更改为此。
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position,
long id) {
if (dataList.get(position).getTitle() == null) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
SelectItem(position);;
}
}, 250);
}
mDrawerList.setItemChecked(possition, true);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
并将SelectItem方法更改为此
public void SelectItem(int possition) {
Fragment fragment = null;
Bundle args = new Bundle();
switch (possition) {
case 2:
fragment = new FragmentZero();
break;
case 3:
fragment = new FragmentOne();
break;
case 4:
fragment = new FragmentTwo();
break;
case 5:
fragment = new FragmentThree();
break;
case 7:
fragment = new FragmentTwo();
break;
case 8:
fragment = new FragmentZero();
break;
case 9:
fragment = new FragmentOne();
break;
case 10:
fragment = new FragmentTwo();
break;
case 11:
fragment = new FragmentZero();
break;
case 12:
fragment = new FragmentOne();
break;
case 14:
fragment = new FragmentZero();
break;
case 15:
fragment = new FragmentOne();
break;
case 16:
fragment = new FragmentTwo();
break;
default:
break;
}
fragment.setArguments(args);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
setTitle(dataList.get(possition).getItemName());
}