how to create smooth navigation drawer

16,688

Solution 1

Not sure this is the best route, but the way I solved this was to create a pending Runnable that runs in onDrawerClosed. Eg:

private void selectItem(final int position) {
    mPendingRunnable = new Runnable() {
        @Override
        public void run() {
            // update the main content by replacing fragments
            Fragment fragment = new PlanetFragment();
            Bundle args = new Bundle();
            args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
            fragment.setArguments(args); 

            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
        }
    });

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void onDrawerClosed(View view) {
    getActionBar().setTitle(mTitle);
    invalidateOptionsMenu();

    // If mPendingRunnable is not null, then add to the message queue 
    if (mPendingRunnable != null) {
        mHandler.post(mPendingRunnable);
        mPendingRunnable = null;
    }
}

Solution 2

Works perfect

private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
            mDrawerLayout.closeDrawer(mDrawerList);
            mDrawerLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    selectItem(position);
                }
            }, 300);
        }
    }

Solution 3

Just posting the drawerLayout.closeDrawer() on the message queue using the handler, with a minimal delay solved my problem. Handler.postDelayed() is the key here.

 public void selectItem(int position)
    {
        switch (position)
        {
            case 0:

                DashboardFragment dashboardFragment = new DashboardFragment();
                Bundle args = new Bundle();
                args.putInt(dashboardFragment.ARG_SCREEN_NUMBER, position);
                dashboardFragment.setArguments(args);

                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

                // Replace whatever is in the fragment_container view with this fragment,
                // and add the transaction to the back stack so the user can navigate back
                transaction.replace(R.id.fragmentLayout, dashboardFragment, TAG_DASHBOARD);
                transaction.addToBackStack(null);

                // Commit the transaction
                transaction.commit();
                getSupportFragmentManager().executePendingTransactions();
                break;            

            default:
                break;

        }

        // Highlight the selected item, update the title, and close the drawer
        drawerList.setItemChecked(position, true);
        setTitle(mScreenTitles[position]);

        mPendingRunnable = new Runnable()
        {
            @Override
            public void run()
            {
                drawerLayout.closeDrawer(GravityCompat.START);
            }
        };
        mHandler.postDelayed(mPendingRunnable,50);

    }
Share:
16,688

Related videos on Youtube

fish40
Author by

fish40

Updated on June 04, 2022

Comments

  • fish40
    fish40 almost 2 years

    I am using this example for navigation drawer. When clicking on of item of left drawer listview it shows some images but drawerLayout closes not smoothly.

    What should I do here for smoothly close left drawer layout after clicking of the item of listview.

    • fish40
      fish40 over 10 years
      @CommonsWare thank you for your answer but what should I find here?
    • fish40
      fish40 over 10 years
      @agamov thanks for your support but I'm noy saying how to create navigation drawer, I mean SMOOTH navigation drawer.
  • Chase Florell
    Chase Florell almost 10 years
    can I ask, where do you get mHandler from? I'm trying to do this in C#, but am hung up on where it's coming from.
  • Paul Burke
    Paul Burke almost 10 years
    @ChaseFlorell Simply create a member variable. E.g. Handler mHandler = new Handler();
  • nucleartide
    nucleartide almost 10 years
    I'd like to add: since onDrawerClosed() is not called upon starting your app, you should also post mPendingRunnable to mHandler at the end of onCreate().
  • theblang
    theblang over 9 years
    @Ewoks Unfortunately, creating artificial delay before loading the next fragment / activity seems to be the only solution if you don't want a jerky close animation.
  • Artem Mostyaev
    Artem Mostyaev about 9 years
    You can do all actions from selectItem in onDrawerClosed. The latest version of Gmail do it in such way.
  • Ewoks
    Ewoks almost 9 years
    @mattblang Paul gave better solution
  • Anand Savjani
    Anand Savjani about 8 years
    such fabulous snippet
  • Nil Llisterri
    Nil Llisterri over 6 years
    Don't forget to implement DrawerLayout.DraweListener in the Activity and set the listener: drawer.setDrawerListener(this);