Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MaterialDrawer CustomDrawerItem

How can I create a custom DrawerItem (1. I need toggle 2. I need boolean state indicator) in the MaterialDrawer library? Here MaterialDrawer author says, that creating custom DrawerItem is a really easy task, but I can't find it in the documentation of this library.

like image 390
igor_rb Avatar asked Aug 18 '15 07:08

igor_rb


1 Answers

To implement a CustomDrawerItem you have to implement the IDrawerItem interface. This defines all methods which are required. Just take a closer look at the DividerDrawerItem or the SectionDrawerItem which are both a bit simpler.

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);
        }
    }
}
like image 164
mikepenz Avatar answered Nov 04 '22 05:11

mikepenz