3

I'm using navigation drawer working fine, but it closing slowly after an item is pressed. This happens if next activity has a extra code in oncreate method otherwise working properly..

So please help to solve this

RAVI VAGHELA
  • 877
  • 1
  • 10
  • 12

4 Answers4

3

I just managed to solve the problem you're experiencing.

First of all i have to say that i'm working on Android Studio 1.1.0 generated NavigationDrawer project.

This is the method onCreateView() of the class NavigationDrawerFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    mDrawerListView = (ListView) inflater.inflate(
            R.layout.fragment_navigation_drawer, container, false);

    mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            selectItem(position);
        }
    });

    mDrawerListView.setAdapter(new ArrayAdapter<String>(
            getActionBar().getThemedContext(),
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1,
            new String[]{
                    getString(R.string.title_section1),
                    getString(R.string.title_section2),
                    getString(R.string.title_section3),
            }));

    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);

    return mDrawerListView;
}

When a Item is clicked the

mDrawerListView.setOnItemClickListener()

callback will fire and then the ball pass to

selectItem(position)

method.

The "selectItem()" method hides the NavigationDrawer and, using a callback, it calls a method into the "MainActivity" class -onNavigationDrawerItemSelected()-, that start the transition to the selected fragment.

The animation stuttering/lag happens because the code tries to close the NavigationDrawer and to get the UI layout hardcore job done at the same time.

If you want to avoid the lag you have to choose what to do first.

In my own workaround i decided to:

  1. Close the NavigationDrawer
  2. Getting the UI (layout) job done

This is my own solution:

mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        final int pos = position;

        mDrawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener(){
            @Override
            public void onDrawerClosed(View drawerView)
            {
                super.onDrawerClosed(drawerView);
                selectItem(pos);
            }
        });

        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }
});

Don't be confused by the code.

We moved the selectItem(pos) inside a callback that will be fired only then the Drawer is closed and then we force the Drawer to close so the magic may occur.

This solution work for me, hope to know if it works for you as well.

Matt
  • 721
  • 9
  • 26
  • 1
    Side note: onDrawerClosed will also be called when user only slide close the drawer. So you better add a flag to make selectItem() only called when user clicked a drawer item. – Dark Leonhart Dec 18 '16 at 09:14
0

There is closeDrawer(View view) and closerDrawer(int gravity). On your item click .closeDrawer(Gravity.START);

work fines for me.They might be helpful you too.

0

in your displayView() you get the selected item from drawer so you can specify the mDrawerLayout gravity like

     private void displayView(int position) {
                // update the main content by replacing fragments
                Fragment fragment = null;
                switch (position) {
                case 0:
                    //load your fragment here
                    mDrawerLayout.closeDrawer(Gravity.LEFT);


                    break;
    }
Bhavik Mehta
  • 573
  • 8
  • 21
0

on your item click you have to close drawaer layout

drawlayout = (DrawerLayout) findViewById(R.id.drawer_layout);

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;

    case 1:
        fragment = new EventFragment();
        break;

    case 2:
        fragment = new SponsorsFragment();
        break;

    case 3:
        fragment = new AboutFragment();
        break;

    case 4:
        fragment = new OurTeamFragment();
        break;

    case 5:
        fragment = new VideoFragment();
        break;

    case 6:
        fragment = new ContactFragment();
        break;

    default:
        break;
    }
    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager()
                .beginTransaction();
        ft.setCustomAnimations(android.R.anim.slide_in_left,
                android.R.anim.slide_out_right);
        ft.replace(R.id.content_frame, fragment);
        ft.commit();
        // pushFragments(fragment, true, true);

        drawlayout.closeDrawers();
    }
}
Akash
  • 961
  • 6
  • 15