Navigation drawer lag

5.7k Views Asked by At

The navigation drawer lags every time I open a new activity. I looked on Google for a solution and I found out that I can solve it by delaying the new activity with a Handler. I experimented a little bit but got nowhere.

Some code pieces from 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);
    }

and

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

There are 1 best solutions below

2
On BEST ANSWER

In your activity create a handler and initialize it in your on create method

private Handler mHandler;
mHandler = new Handler();

then change your drawer item click listener to this.

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

and change SelectItem method to this

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