mikepenz / MaterialDrawer

The flexible, easy to use, all in one drawer library for your Android project. Now brand new with material 2 design.
https://mikepenz.dev
Apache License 2.0
11.67k stars 2.05k forks source link

Clicking on a subItem of an ExpandableDrawerItem causes the clousure of the Drawer #1848

Closed LorenzoZoia closed 7 years ago

LorenzoZoia commented 7 years ago

I am encountering a strange problem. Despite I set .withCloseOnClick(false) when I click on an subItem of an ExpandableDrawerItem and I start a new activity, the entire Drawer get closed.

mikepenz commented 7 years ago

@LorenzoZoia you could consume the event in the click listener if it's a subitem

LorenzoZoia commented 7 years ago

Ok, how can I do that when I set the touch listener on the drawer ??

mikepenz commented 7 years ago

@LorenzoZoia don'T you have a DrawerItemClickListener to do something when the user clicks an item?

LorenzoZoia commented 7 years ago

Yes, I am using the OnDrawerItemClickListener to start a new activity when I click on the subItem

LorenzoZoia commented 7 years ago

This is my Drawer :

    //Creation of the Drawer
    final Drawer result = new DrawerBuilder()
            .withActivity(fatherActivity)
            .withHeader(R.layout.menu_header)
            .withToolbar(toolbar)
            .withCloseOnClick(false)
            .withSliderBackgroundColor(fatherActivity.getResources().getColor(windowBackground))
            .addDrawerItems(
                    item8,
                    item,
                    item1,
                    item2,
                    item3,
                    item4,
                    item5,
                    new DividerDrawerItem(),
                    item6,
                    item7
            )
            .build();
    result.setSelection(-1);

    result.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
        @Override
        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
            if(drawerItem != null){
                Intent intent=null;
                if(drawerItem.getIdentifier() == 4 && !(fatherActivity instanceof BottleBeerListActivity)){
                    intent = new Intent(fatherActivity, BottleBeerListActivity.class);
                }
                else if(drawerItem.getIdentifier() == 10 && !(fatherActivity instanceof HomeActivity)){
                    intent = new Intent(fatherActivity, HomeActivity.class);
                }

                if(intent != null){
                    fatherActivity.startActivity(intent);
                    fatherActivity.overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right);
                }
            }
            return true;
        }
    });
    return result;

And this is how I create the Expandable:

PrimaryDrawerItem subItem1 = new PrimaryDrawerItem().withIdentifier(3).withName(R.string.drawer_item_spina).withLevel(2).withTextColor(Color.WHITE); subItem1.withIcon(R.drawable.ic_beer_tap); PrimaryDrawerItem subItem2 = new PrimaryDrawerItem().withIdentifier(4).withName(R.string.drawer_item_bottle).withLevel(2).withTextColor(Color.WHITE); subItem2.withIcon(R.drawable.ic_beerbottle); ExpandableDrawerItem item = new ExpandableDrawerItem().withName(R.string.drawer_item_beer).withTextColor(Color.WHITE).withSelectable(false).withSubItems( subItem1, subItem2 ).withIcon(R.drawable.ic_beer);

mikepenz commented 7 years ago

Setting an ExpandableItem withSelectable(false) will also prevent the closing, as shown in the DrawerActivity of the sample app: https://github.com/mikepenz/MaterialDrawer/blob/develop/app/src/main/java/com/mikepenz/materialdrawer/app/DrawerActivity.java#L138