Я хочу добавить NavigationDrawer ко всем моим занятиям. Я не удовлетворен многими решениями, которые я нашел в stackoverflow и других форумах. Затем я нашел это MaterialDrawer library, который, кажется, делает его очень легким. Но знаю, что я хотел бы реализовать собственный стиль для всех элементов в ящике. Как я могу сделать это с помощью этой библиотеки?NavigationDrawer в нескольких видах деятельности и стилях
ответ
MaterialDrawer
построен с гибкостью в виду. Он принимает объекты, которые соединяют интерфейс IDrawerItem
. Таким образом, вы можете легко добавить свои собственные элементы, просто используя интерфейс IDrawerItem
. Есть уже по умолчанию DrawerItems
, как PrimaryDrawerItem
, на которые вы можете положиться (просто растяните его), или вы можете пойти с полным пользовательским подходом.
Для упрощения работы MaterialDrawer
поставляется с AbstractDrawerItem
, который уже реализует поведение по умолчанию, необходимое для большинства элементов.
Вы можете проверить пользовательские элементы DrawerItems, добавленные в примере приложения here.
Полный пользовательский элемент является 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()
, который будет вызываться для установки данных во все виды.