如何在 mikepenz materialdrawer 库的帐户资料部分添加分隔符?

How to add a divider to the account profile section of mikepenz materialdrawer library?

如何将分隔符添加到 mikepenz materialdrawer 库的帐户配置文件部分?

我可以用new DividerDrawerItem()给抽屉本身加一个隔板,但是没办法给配置文件部分加一个隔板。我该怎么做?

hmmm 要在个人资料部分中获得一个分隔线,最简单的方法是创建一个类似于 DividerDrawerItem 的项目,但它也应该实现 IProfile,这样您也可以将它添加到帐户中,您只需要让它不可选,因此它不会出现在 header 中,也无法被选中。

public class AccountDividerDrawerItem extends AbstractDrawerItem<AccountDividerDrawerItem, AccountDividerDrawerItem.ViewHolder> implements IProfile<AccountDividerDrawerItem> {
@Override
public int getType() {
    return R.id.material_drawer_profile_item_divider;
}

@Override
@LayoutRes
public int getLayoutRes() {
    return com.mikepenz.materialdrawer.R.layout.material_drawer_item_divider;
}

@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());

    //define how the divider should look like
    viewHolder.view.setClickable(false);
    viewHolder.view.setEnabled(false);
    viewHolder.view.setMinimumHeight(1);
    ViewCompat.setImportantForAccessibility(viewHolder.view,
            ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO);

    //set the color for the divider
    viewHolder.divider.setBackgroundColor(UIUtils.getThemeColorFromAttrOrRes(ctx, com.mikepenz.materialdrawer.R.attr.material_drawer_divider, com.mikepenz.materialdrawer.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, viewHolder.itemView);
}

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

@Override
public AccountDividerDrawerItem withName(String name) {
    return null;
}

@Override
public StringHolder getName() {
    return null;
}

@Override
public AccountDividerDrawerItem withEmail(String email) {
    return null;
}

@Override
public StringHolder getEmail() {
    return null;
}

@Override
public AccountDividerDrawerItem withIcon(Drawable icon) {
    return null;
}

@Override
public AccountDividerDrawerItem withIcon(Bitmap bitmap) {
    return null;
}

@Override
public AccountDividerDrawerItem withIcon(@DrawableRes int iconRes) {
    return null;
}

@Override
public AccountDividerDrawerItem withIcon(String url) {
    return null;
}

@Override
public AccountDividerDrawerItem withIcon(Uri uri) {
    return null;
}

@Override
public AccountDividerDrawerItem withIcon(IIcon icon) {
    return null;
}

@Override
public ImageHolder getIcon() {
    return null;
}

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;
    private View divider;

    private ViewHolder(View view) {
        super(view);
        this.view = view;
        this.divider = view.findViewById(com.mikepenz.materialdrawer.R.id.material_drawer_divider);
    }
}
}

只有两件事!

  1. 您的项目还应该具有为 AbstractDrawerItem 定义的正确通用类型。
  2. 您应该在 getType() 方法中更改标识符,因为它必须是唯一的并且不能跨项目重复使用。 (RecyclerView 的内部缓存处理)。

注意R.id.material_drawer_profile_item_divider不存在,必须替换为任意本地identifier!所以在 values 目录中创建 ids.xml 文件并将此行添加到该目录中:

<item name="material_drawer_profile_item_divider" type="id" />