gpt4 book ai didi

android - 抽屉导航滞后

转载 作者:行者123 更新时间:2023-11-29 00:15:44 25 4
gpt4 key购买 nike

每次打开新 Activity 时,抽屉导航都会滞后。我在谷歌上寻找解决方案,发现我可以通过使用处理程序延迟新 Activity 来解决它。我尝试了一点,但一无所获。

MainActivity.java 中的一些代码 fragment :

 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);
}
}
}

最佳答案

在您的 Activity 中创建一个处理程序并在您的创建方法中对其进行初始化

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());
}

关于android - 抽屉导航滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27018767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com