0

I want to add a NavigationDrawer to all of my Activities. I am unsatisfied with many solutions I found on stackoverflow and other forums. Then I found this MaterialDrawer library which seems to make it very easy. But know I would like to implement an own style for all items in the Drawer. How can I do that with this library?

unlimited101
  • 3,653
  • 4
  • 22
  • 41

1 Answers1

0

The MaterialDrawer is built with flexibility in mind. It takes objects which imlements the IDrawerItem interface. So you can easily add your own elements just by implementing the IDrawerItem interface. There are already some default DrawerItems like the PrimaryDrawerItem which you can depend on (just extend it) or you can go with a complete custom approach.

To make things easier the MaterialDrawer comes with the AbstractDrawerItem which already implements default behaviors needed for most items.

You can check out the custom DrawerItems added in the sample application here.

A complete custom item is the IconDrawerItem

public class IconDrawerItem extends AbstractDrawerItem<IconDrawerItem, IconDrawerItem.ViewHolder> {
    protected ImageHolder icon;
    protected ImageHolder selectedIcon;

    protected boolean iconTinted = false;

    protected ColorHolder iconColor;
    protected ColorHolder selectedIconColor;
    protected ColorHolder disabledIconColor;

    //GETTER & SETTER REMOVED TO KEEP THE SNIPPET SMALL

    @Override
    public int getType() {
        return R.id.material_drawer_item_icon_only;
    }

    @Override
    @LayoutRes
    public int getLayoutRes() {
        return R.layout.material_drawer_item_icon_only;
    }

    @Override
    public void bindView(ViewHolder viewHolder) {
        Context ctx = viewHolder.itemView.getContext();

        //set the identifier from the drawerItem here. It can be used to run tests
        viewHolder.itemView.setId(hashCode());

        //get the correct color for the icon
        int iconColor;
        if (this.isEnabled()) {
            iconColor = ColorHolder.color(getIconColor(), ctx, R.attr.material_drawer_primary_icon, R.color.material_drawer_primary_icon);
        } else {
            iconColor = ColorHolder.color(getDisabledIconColor(), ctx, R.attr.material_drawer_hint_icon, R.color.material_drawer_hint_icon);
        }
        int selectedIconColor = ColorHolder.color(getSelectedIconColor(), ctx, R.attr.material_drawer_selected_text, R.color.material_drawer_selected_text);

        //get the drawables for our icon and set it
        Drawable icon = ImageHolder.decideIcon(getIcon(), ctx, iconColor, isIconTinted(), 1);
        Drawable selectedIcon = ImageHolder.decideIcon(getSelectedIcon(), ctx, selectedIconColor, isIconTinted(), 1);
        ImageHolder.applyMultiIconTo(icon, iconColor, selectedIcon, selectedIconColor, isIconTinted(), viewHolder.icon);

        //call the onPostBindView method to trigger post bind view actions (like the listener to modify the item if required)
        onPostBindView(this, viewHolder.itemView);
    }

    @Override
    public ViewHolderFactory getFactory() {
        return new ItemFactory();
    }

    public static class ItemFactory implements ViewHolderFactory<ViewHolder> {
        public ViewHolder create(View v) {
            return new ViewHolder(v);
        }
    }

    protected static class ViewHolder extends RecyclerView.ViewHolder {
        private View view;
        protected ImageView icon;

        private ViewHolder(View view) {
            super(view);
            this.view = view;
            this.icon = (ImageView) view.findViewById(R.id.material_drawer_icon);
        }
    }
}

There are no limitations. You just have to define the type as identifier, the layout which is used, and implement the bindView() which will be called to set the data to all views.

mikepenz
  • 12,708
  • 14
  • 77
  • 117