androidbadgematerialdrawer

updateBadge in expandableitem in MaterialDrawer Android


I show a badge in the expandableItem. When the user click in that item, I want that the badge in the expandableItem disappeare and just show the badge in the subitem. But in my code, when I do click, the badge disappear but the subitems are not shown. What I am doing wrong? Thanks.

result = new DrawerBuilder()
            .withActivity(this)
            .withToolbar(appbar)
            .withHasStableIds(true)
            .withAccountHeader(headerResult) 
            .addDrawerItems(
                    ...

                    new ExpandableBadgeDrawerItem().withName("Collapsable Badge").withIcon(R.drawable.ic_menu_exp).withIdentifier(18).withSelectable(false).withBadge("2").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_red_700)).withSubItems(
                            new SecondaryDrawerItem().withName("CollapsableItem").withLevel(2).withIcon(R.drawable.ic_menu_send).withIdentifier(2000).withBadge("2").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_red_700)),
                            new SecondaryDrawerItem().withName("CollapsableItem 2").withLevel(2).withIcon(R.drawable.ic_menu_send).withIdentifier(2001)
                    )
            ) 
            .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                @Override
                public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {


                    if (drawerItem != null) {

                        if (drawerItem.getIdentifier() == 18) {
                            result.updateBadge(18, null);
                        }
                    }

                    return false;
                }
            })
            .withSavedInstance(savedInstanceState)
            .build();

Solution

  • I checked it and you are right, it's basically a result of the way how the underlaying FastAdapter handle's expandable items, if the parent is updated it will collapse the child's. This will be improved in v3 of the FastAdapter until then please use the following snippet:

    //get the item from our drawerItem
    ExpandableBadgeDrawerItem d = (ExpandableBadgeDrawerItem) drawerItem;
    //remove the badge
    d.withBadge((StringHolder) null);
    //notify the adapter that the item was changed
    result.getAdapter().notifyItemChanged(result.getPosition(18));