0

I have a drawerLayout (support.v4) with a navigationView and have many (like many up to n menu items) menu items. To have less items on the screen at the same time, some menu items lead to another menu with many items.

The thing is, my menu switch works fine, I'm able to pass from one menu to the other one but I have no animation... I want to close the drawer when an item linked to another menu is selected and once the menu is loaded, to open it again.

Here's my code:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem selectedItem) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
            Log.d(MainActivity.class.getName(), selectedItem.toString());
            MobileXMLMenu menu = UserMenu.getInstance().getMenu();
            for(int i = 0; i < menu.sizeMenuItems(); i++){
                if(menu.getMenuItem(i).getLabel(Utility.retreiveLanguage()).equals(selectedItem.toString())){
                    Log.d(MainActivity.class.getName(), menu.getMenuItem(i).sizeMenuItems() + "");
                    if(menu.getMenuItem(i).sizeMenuItems() > 0){

                        Menu nMenu = navigationView.getMenu();
                        nMenu.clear();
                        for(int j = 0; j < menu.getMenuItem(i).sizeMenuItems(); j++){
                            MobileXMLMenuItem item = menu.getMenuItem(i).getMenuItem(j);
                            nMenu.add(item.getLabel(Utility.retreiveLanguage())).setIcon(MemberOptionsManager.getInstance().getMenuItemIcon(item.getDecorator()));
                        }
                        mDrawerLayout.openDrawer(GravityCompat.START);
                        isInSubmenu = true;
                    }
                }
            }
            return false;
        }
    });

I also have this for my ActionBarDrawerToggle (don't know if this could help):

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
        public void onDrawerClosed(View view){
            super.onDrawerClosed(view);
            getSupportActionBar().setTitle(mFragment.getFragmentName());
            invalidateOptionsMenu();
            mDrawerToggle.syncState();
        }
        public void onDrawerOpened(View drawerView){
            super.onDrawerClosed(drawerView);
            getSupportActionBar().setTitle("Open");
            invalidateOptionsMenu();
            mDrawerToggle.syncState();
        }
    };

When I select an item not link to any sub menu, the drawer closes, but otherwise, I see no animation, just the menu content changing with no animation. How can I force my app to wait for the drawer to close before changing the menu content?

Thanks!

dequec64
  • 923
  • 1
  • 8
  • 26

2 Answers2

0

You can use callback methods of the drawer layout from official documentation of the android.

 onDrawerClosed(View drawerView)
 onDrawerOpened(View drawerView)
 onDrawerSlide(View drawerView, float slideOffset)
 onDrawerStateChanged(int newState)

You can find more at link:http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.DrawerListener.html

rmammadli
  • 76
  • 1
  • 7
0

You can setDrawerListener on DrawLayout like this

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
            public void onDrawerClosed(View view) {
                handleClickNavigationItem(mClickedNavigationItem);
            }

            public void onDrawerOpened(View drawerView) {
            }
        };
        mDrawerLayout.setDrawerListener(toggle);

handleClickNavigationItem method contains the code which handle navigation event.

And just save clicked item in onNavigationItemSelected method

public boolean onNavigationItemSelected(MenuItem item) {
        mClickedNavigationItem = item.getItemId();
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
    return true;
}
topcbl
  • 799
  • 6
  • 20