如何使用lib mikepenz / MaterialDrawer自定义DrawerItem

时间:2015-06-24 15:22:27

标签: android materialdrawer

我正在使用lib MaterialDrawer(https://github.com/mikepenz/MaterialDrawer)。

我想使用右侧的侧栏作为过滤器部分,如Foursquare过滤器:

enter image description here

但我注意到只有SectionDrawerItem.java,SwitchDrawerItem.java和ToggleDrawerItem.java定义了切换和切换。但它们不足以定义过滤器。我想问一下我是否可以为侧边栏定义自己的布局或者为DrawerItem添加更多选项?提前谢谢!

2 个答案:

答案 0 :(得分:11)

最简单的解决方案是扩展现有的DrawerItems之一,但这只适用于您不需要完全不同的项目的情况。

示例应用程序中已显示CustomDrawerItem

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);
        }
    }
}

如果您需要更多自定义,只需实现IDrawerItem接口并实现这些方法。实现DrawerItem的更简单的AbstractDrawerItemDividerDrawerItem

,其中包含一些预定义的方法和属性
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);
        }
    }
}

答案 1 :(得分:1)

我创建了一个扩展BaseDrawerItem的类来自定义DrawerItem。它有效。