8

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

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

在此处输入图像描述

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

4

2 回答 2

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接口并实现方法。DividerDrawerItem是一个更容易DrawerItem实现的AbstractDrawerItem带有一些预定义方法和属性的

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);
        }
    }
}
于 2015-09-12T19:47:12.747 回答
1

我创建了一个扩展 BaseDrawerItem 以自定义 DrawerItem 的类。有用。

于 2015-06-25T14:26:02.973 回答