导航抽屉滞后



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

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

在您的活动中创建一个处理程序并在您的创建方法中初始化它

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

并将选择项方法更改为此

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

相关内容

  • 没有找到相关文章

最新更新