How to configure DrawerItem using lib mikepenz / MaterialDrawer

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

I would like to use the sidebar on the right as a filter section, such as a Foursquare filter:

enter image description here

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

+6
source share
2 answers

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

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 IDrawerItem interface and implement the methods. A simpler DrawerItem that implements AbstractDrawerItem that comes with several predefined methods and properties is 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); } } } 
+11
source

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

+1
source

Source: https://habr.com/ru/post/989686/


All Articles