Materialdrawer: How do I get my drawer to close onClick?

Created on 30 May 2016  路  14Comments  路  Source: mikepenz/MaterialDrawer

            .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                @Override
                public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                    return false;
                }
            })
question

Most helpful comment

All 14 comments

@alexf1 you do not consume the event, and you enable it on the builder.

@mikepenz the onclick works I just can't get it to close when it fires

Something must be blocking it

    final Drawer result = new DrawerBuilder()
            .withActivity(this)
            .withToolbar(toolbar)
            .withTranslucentStatusBar(true)
            .withAccountHeader(headerResult)
            .withDrawerLayout(crossfadeDrawerLayout)
            .withHasStableIds(true)
            .withDrawerWidthDp(72)
            .withGenerateMiniDrawer(true)
            .addDrawerItems(
                    settings,
                    new DividerDrawerItem(),
                    portal,
                    accounts,
                    contacts,
                    opportunities,
                    history,
                    orders,
                    orderlines,
                    quotes,
                    parts,
                    dashboard

            )
            .withSelectedItem(-1)
            .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                @Override
                public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                    populateSpinner(drawerItem.getIdentifier());
                    return false;
                }
            })
            .withCloseOnClick(true)
            .withSavedInstance(savedInstanceState)
            .build();

@alexf1 oh I think in your case it's the fact that you use the CrossfadeDrawerLayout and the miniDrawer.

I assume you want it to close when the MiniDrawer is clicked?

I believe so yes

@alexf1 ok for the MiniDrawer to close the DrawerLayout you have to do the following:

        miniResult.withOnMiniDrawerItemClickListener(new FastAdapter.OnClickListener<IDrawerItem>() {
            @Override
            public boolean onClick(View v, IAdapter<IDrawerItem> adapter, IDrawerItem item, int position) {
                result.closeDrawer();
                return false;
            }
        });

@mikepenz Still doesn't close for some reason. Neither the mini or normal drawer close on click

@alexf1 I added this code to the CrossfadeDrawerActivity of the sample application and it worked. Anything else you have defined?

Above is my whole builder, I'm not sure what else to show you

@alexf1 where did you define the listener from above? You can compare it with the CrossfadeDrawerLayout sample from here:
https://github.com/mikepenz/MaterialDrawer/blob/develop/app/src/main/java/com/mikepenz/materialdrawer/app/CrossfadeDrawerLayoutActvitiy.java#L108

I just added the above code in line 108 as linked above:

        miniResult.withOnMiniDrawerItemClickListener(new FastAdapter.OnClickListener<IDrawerItem>() {
            @Override
            public boolean onClick(View v, IAdapter<IDrawerItem> adapter, IDrawerItem item, int position) {
                result.closeDrawer();
                return false;
            }
        });
    //define maxDrawerWidth (this is the width in the complete opened state)
    crossfadeDrawerLayout.setMaxWidthPx(DrawerUIUtils.getOptimalDrawerWidth(this));

    //add second view (which is the miniDrawer)
    MiniDrawer miniResult = result.getMiniDrawer();
    //build the view for the MiniDrawer
    View view = miniResult.build(this);
    //set the background of the MiniDrawer as this would be transparent
    view.setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(this, com.mikepenz.materialdrawer.R.attr.material_drawer_background, com.mikepenz.materialdrawer.R.color.material_drawer_background));
    //we do not have the MiniDrawer view during CrossfadeDrawerLayout creation so we will add it here
    crossfadeDrawerLayout.getSmallView().addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    //define the crossfader to be used with the miniDrawer. This is required to be able to automatically toggle open / close
    miniResult.withCrossFader(new ICrossfader() {
        @Override
        public void crossfade() {
            crossfadeDrawerLayout.crossfade(400);
            //only close the drawer if we were already faded and want to close it now
            if (isCrossfaded()) {
                result.getDrawerLayout().closeDrawer(GravityCompat.START);
            }
        }

        @Override
        public boolean isCrossfaded() {
            return crossfadeDrawerLayout.isCrossfaded();
        }
    });

If I get rid of the mini drawer it closes as expected

@alexf1 hmmm I am not sure, as mentioned I tested it in the sample application and there it worked.

You may can send me the part of the project so I can debug it what may differes on your side

I think I'll just get rid of the mini-drawer all together since the normal one seems to work fine

Was this page helpful?
0 / 5 - 0 ratings

Related issues

y2kshane picture y2kshane  路  4Comments

armoun picture armoun  路  3Comments

GutoMartins019 picture GutoMartins019  路  4Comments

jehad-suliman picture jehad-suliman  路  3Comments

jd-alexander picture jd-alexander  路  4Comments