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

前端之家收集整理的这篇文章主要介绍了android – 如何使用lib mikepenz / MaterialDrawer自定义DrawerItem前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用lib MaterialDrawer( https://github.com/mikepenz/MaterialDrawer).

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

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

解决方法

最简单的解决方案是扩展现有的DrawerItem之一,但这仅在您不需要完全不同的项时才有效.

示例应用程序中已显示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接口并实现这些方法.实现带有一些预定义方法属性的AbstractDrawerItem的更简单的DrawerItem是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);
        }
    }
}
原文链接:https://www.f2er.com/android/310304.html

猜你在找的Android相关文章