8

I am using the lib MaterialDrawer (https://github.com/mikepenz/MaterialDrawer).

I would like to use the side bar on the right as the filter section like Foursquare filter:

enter image description here

But I noticed that there are only SectionDrawerItem.java, SwitchDrawerItem.java and ToggleDrawerItem.java that defined toggle and switch. But they are not enough to define the filters. I would like to ask if I can define my own layout for the sidebar or add more options to the DrawerItem? Thanks in advance!

Hamed Ghadirian
  • 6,159
  • 7
  • 48
  • 67
Shi
  • 510
  • 1
  • 5
  • 16

2 Answers2

11

The easiest solution is to extend one of the existing DrawerItems, but this only works if you do not need a completely different item.

A CustomDrawerItem is already shown in the sample application

public class CustomPrimaryDrawerItem extends PrimaryDrawerItem {

    private ColorHolder background;

    public CustomPrimaryDrawerItem withBackgroundColor(int backgroundColor) {
        this.background = ColorHolder.fromColor(backgroundColor);
        return this;
    }

    public CustomPrimaryDrawerItem withBackgroundRes(int backgroundRes) {
        this.background = ColorHolder.fromColorRes(backgroundRes);
        return this;
    }

    @Override
    public void bindView(RecyclerView.ViewHolder holder) {
        super.bindView(holder);

        if (background != null) {
            background.applyToBackground(holder.itemView);
        }
    }
}

If you need more customization just implement the IDrawerItem interface and implement the methods. An easier DrawerItem which implements the AbstractDrawerItem which comes with a few predefined methods and properties is the DividerDrawerItem

public class DividerDrawerItem extends AbstractDrawerItem<DividerDrawerItem> {
    @Override
    public String getType() {
        return "DIVIDER_ITEM";
    }

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

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

        //get our viewHolder
        ViewHolder viewHolder = (ViewHolder) holder;

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

        //define how the divider should look like
        viewHolder.view.setClickable(false);
        viewHolder.view.setEnabled(false);
        viewHolder.view.setMinimumHeight(1);

        //set the color for the divider
        viewHolder.divider.setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(ctx, R.attr.material_drawer_divider, R.color.material_drawer_divider));

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

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

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

    private static class ViewHolder extends RecyclerView.ViewHolder {
        private View view;
        private View divider;

        private ViewHolder(View view) {
            super(view);
            this.view = view;
            this.divider = view.findViewById(R.id.material_drawer_divider);
        }
    }
}
diaa
  • 316
  • 2
  • 15
mikepenz
  • 12,708
  • 14
  • 77
  • 117
  • 1
    I created a class that extends BaseDrawerItem<> instead of AbstractDrawerItem<>, it works well for me. I would like to ask what's the differences between these two class? – Shi Sep 21 '15 at 14:48
  • 1
    The AbstractDrawerItem just implements some things useable in more common DrawerItems like the SectionDrawerItem as those do not have an icon or so on, the BaseDrawerItem adds those things which are used in the Primary, Secondary, Toggle Switch DrawerItems which do have an icon. And then there are the PrimaryBaseDrawerItem (and secondary) which is the common base for the primary, switch, toggle items, and the same again for the secondary :D So we do not have to much duplicated code ;) – mikepenz Sep 21 '15 at 19:58
  • CustomDrawerItem https://github.com/mikepenz/MaterialDrawer/blob/develop/app/src/main/java/com/mikepenz/materialdrawer/app/drawerItems/CustomPrimaryDrawerItem.java updated link – Shubham AgaRwal May 20 '16 at 06:24
1

I created a class that extends BaseDrawerItem to customize DrawerItem . it works.

Shi
  • 510
  • 1
  • 5
  • 16