NavigationDrawer在多个活动和样式行中

时间:2016-04-13 13:46:58

标签: android material-design navigation-drawer materialdrawer

我想在我的所有活动中添加一个NavigationDrawer。我对stackoverflow和其他论坛上发现的许多解决方案都不满意。然后我发现这个MaterialDrawer library似乎很容易。但是知道我想为Drawer中的所有项目实现自己的样式。我怎么能用这个库做到这一点?

1 个答案:

答案 0 :(得分:0)

MaterialDrawer在构建时考虑了灵活性。它需要能够实现IDrawerItem接口的对象。因此,只需实现IDrawerItem接口即可轻松添加自己的元素。已经存在一些默认DrawerItems,例如您可以依赖的PrimaryDrawerItem(只需扩展它),或者您可以使用完整的自定义方法。

为了简化操作,MaterialDrawer附带AbstractDrawerItem,它已经实现了大多数项目所需的默认行为。

您可以查看示例应用程序here中添加的自定义DrawerItem。

完整的自定义项目是IconDrawerItem

public class IconDrawerItem extends AbstractDrawerItem<IconDrawerItem, IconDrawerItem.ViewHolder> {
    protected ImageHolder icon;
    protected ImageHolder selectedIcon;

    protected boolean iconTinted = false;

    protected ColorHolder iconColor;
    protected ColorHolder selectedIconColor;
    protected ColorHolder disabledIconColor;

    //GETTER & SETTER REMOVED TO KEEP THE SNIPPET SMALL

    @Override
    public int getType() {
        return R.id.material_drawer_item_icon_only;
    }

    @Override
    @LayoutRes
    public int getLayoutRes() {
        return R.layout.material_drawer_item_icon_only;
    }

    @Override
    public void bindView(ViewHolder viewHolder) {
        Context ctx = viewHolder.itemView.getContext();

        //set the identifier from the drawerItem here. It can be used to run tests
        viewHolder.itemView.setId(hashCode());

        //get the correct color for the icon
        int iconColor;
        if (this.isEnabled()) {
            iconColor = ColorHolder.color(getIconColor(), ctx, R.attr.material_drawer_primary_icon, R.color.material_drawer_primary_icon);
        } else {
            iconColor = ColorHolder.color(getDisabledIconColor(), ctx, R.attr.material_drawer_hint_icon, R.color.material_drawer_hint_icon);
        }
        int selectedIconColor = ColorHolder.color(getSelectedIconColor(), ctx, R.attr.material_drawer_selected_text, R.color.material_drawer_selected_text);

        //get the drawables for our icon and set it
        Drawable icon = ImageHolder.decideIcon(getIcon(), ctx, iconColor, isIconTinted(), 1);
        Drawable selectedIcon = ImageHolder.decideIcon(getSelectedIcon(), ctx, selectedIconColor, isIconTinted(), 1);
        ImageHolder.applyMultiIconTo(icon, iconColor, selectedIcon, selectedIconColor, isIconTinted(), viewHolder.icon);

        //call the onPostBindView method to trigger post bind view actions (like the listener to modify the item if required)
        onPostBindView(this, viewHolder.itemView);
    }

    @Override
    public ViewHolderFactory getFactory() {
        return new ItemFactory();
    }

    public static class ItemFactory implements ViewHolderFactory<ViewHolder> {
        public ViewHolder create(View v) {
            return new ViewHolder(v);
        }
    }

    protected static class ViewHolder extends RecyclerView.ViewHolder {
        private View view;
        protected ImageView icon;

        private ViewHolder(View view) {
            super(view);
            this.view = view;
            this.icon = (ImageView) view.findViewById(R.id.material_drawer_icon);
        }
    }
}

没有限制。您只需将类型定义为标识符,使用的布局,并实现bindView(),将调用该<!DOCTYPE html> <html> <body> <p>Hello World</p> </body> </html> 将数据设置为所有视图。