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);
}
Related videos on Youtube
Author by
fish40
Updated on June 04, 2022Comments
-
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 over 10 years@CommonsWare thank you for your answer but what should I find here?
-
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 almost 10 yearscan 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 almost 10 years@ChaseFlorell Simply create a member variable. E.g.
Handler mHandler = new Handler();
-
nucleartide almost 10 yearsI'd like to add: since
onDrawerClosed()
is not called upon starting your app, you should also postmPendingRunnable
tomHandler
at the end ofonCreate()
. -
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 about 9 yearsYou can do all actions from
selectItem
inonDrawerClosed
. The latest version of Gmail do it in such way. -
Ewoks almost 9 years@mattblang Paul gave better solution
-
Anand Savjani about 8 yearssuch fabulous snippet
-
Nil Llisterri over 6 yearsDon't forget to implement
DrawerLayout.DraweListener
in the Activity and set the listener:drawer.setDrawerListener(this);