如何更新自定义 PrimaryDrawerItem 中的视图?

How to update views in a custom PrimaryDrawerItem?

我正在使用 MaterialDrawer(以及 FastAdapter)在我的应用程序中创建左右抽屉,目前我在右抽屉中使用紧凑型帐户 header(请参阅带有用户照片的绿色区域下面的屏幕截图)。

但是我在那里需要更多的文本字段,所以我想切换 - 并使用自定义 PrimaryDrawerItem(请参见下面屏幕截图中的洋红色区域)。

我研究了 https://github.com/mikepenz/MaterialDrawer/blob/develop/FAQ/howto_modify_add_custom_draweritems.md 中列出的指南,并尝试在此处使用一个 ImageView 和两个 TextView 创建这样的项目:

public class RightDrawerItem extends AbstractDrawerItem<RightDrawerItem, RightDrawerItem.ViewHolder> {

    public String photo;
    public String given;
    public String score;

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

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

    @Override
    public void bindView(ViewHolder vh, List<Object> payloads) {
        super.bindView(vh, payloads);

        Context ctx = vh.itemView.getContext();
        if (ctx.getApplicationContext() instanceof SlovaApplication) {
            SlovaApplication app = (SlovaApplication) ctx.getApplicationContext();

            if (URLUtil.isHttpsUrl(photo)) {
                app.getPicasso()
                        .load(photo)
                        .placeholder(R.drawable.account_gray)
                        .into(vh.mPhoto);
            }
        }

        vh.mGiven.setText(given);
        vh.mScore.setText(score);

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

    @Override
    public ViewHolder getViewHolder(View v) {
        return new ViewHolder(v);
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public void setGiven(String given) {
        this.given = given;
    }

    public void setScore(String score) {
        this.score = score;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mPhoto;
        private TextView mGiven;
        private TextView mScore;

        private ViewHolder(View view) {
            super(view);

            view.setBackgroundColor(Color.MAGENTA);

            mPhoto = view.findViewById(R.id.photo);
            mGiven = view.findViewById(R.id.given);
            mScore = view.findViewById(R.id.score);
        }
    }
}

我的问题是当我打电话时

mRightDrawerItem.setPhoto(...);
mRightDrawerItem.setGiven(...);
mRightDrawerItem.setScore(...);

然后我的自定义 PrimaryDrawerItem 的视图没有更新,如您在屏幕截图的洋红色区域中所见(请原谅 non-english 文本):

此外,我不太确定 vh.itemView 到底是什么?

我也在 Github issue 2349

中问过我的问题

@Alexander Farber 因为 Drawer 只不过是一个带有元素的 RecyclerView(因此这些项目基于与您为 FastAdapter 创建的项目相同的方法(理论上 fastAdapter 项目可以通过另外实现 IDrawerItem 接口来使用, 以及任何列表中的抽屉项目))

这意味着无论何时调整元素的字段,都需要通知适配器有关项目的更新。 MaterialDrawer 带有方便的功能,例如:https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/Drawer.java#L654

drawerItem.doMyUpdate(); // modify the item
Drawer.updateItem(drawerItem); // notify the drawer about the update

但您也可以转到适配器的根目录。获取适配器:https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/Drawer.java#L654 然后从任何适配器更新您所知道的项目。

此外,请务必注意,您可能希望提供 payload 以优化刷新。 (使用自定义负载,然后通过更新视图对该特定负载做出反应)