gpt4 book ai didi

android - MaterialDrawer CustomDrawerItem

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:20:05 27 4
gpt4 key购买 nike

如何在 MaterialDrawer 中创建自定义 DrawerItem(1. 我需要切换 2. 我需要 bool 状态指示器)图书馆? Here MaterialDrawer 作者说,创建自定义 DrawerItem 是一项非常容易的任务,但我在这个库的文档中找不到它。

最佳答案

要实现 CustomDrawerItem,您必须实现 IDrawerItem 接口(interface)。这定义了所有需要的方法。仔细看看 DividerDrawerItemSectionDrawerItem 两者都更简单一些。

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);
}
}
}

关于android - MaterialDrawer CustomDrawerItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32066618/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com