0
我想爲所有活動添加一個NavigationDrawer。我不滿意我在stackoverflow和其他論壇上找到的許多解決方案。然後我發現這個MaterialDrawer library這看起來很容易。但知道我想爲抽屜中的所有物品實現自己的樣式。我該怎麼用這個庫來做到這一點?NavigationDrawer在多個活動和樣式行中
我想爲所有活動添加一個NavigationDrawer。我不滿意我在stackoverflow和其他論壇上找到的許多解決方案。然後我發現這個MaterialDrawer library這看起來很容易。但知道我想爲抽屜中的所有物品實現自己的樣式。我該怎麼用這個庫來做到這一點?NavigationDrawer在多個活動和樣式行中
MaterialDrawer
建立在靈活性的基礎上。它需要對接口進行分配的對象。所以你可以通過實現IDrawerItem
界面輕鬆添加自己的元素。已經有一些默認DrawerItems
,如PrimaryDrawerItem
,您可以依賴(只是擴展它),或者您可以使用完整的自定義方法。
爲了讓事情變得更容易,MaterialDrawer
附帶了AbstractDrawerItem
,它已經實現了大多數項目所需的默認行爲。
您可以查看示例應用程序here中添加的自定義DrawerItems。
一個完整的定製產品的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()
。