代码之家  ›  专栏  ›  技术社区  ›  Alexander Farber

如何更新自定义主抽屉中的视图?

  •  0
  • Alexander Farber  · 技术社区  · 6 年前

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

    不过,我需要更多的文本字段,所以我想切换-并使用一个自定义的主抽屉(请参阅下面屏幕截图中的洋红色区域)。

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

    然后视图my custom PrimaryPaweritem将不会更新,正如您在屏幕截图的洋红色区域中看到的那样(请原谅非英文文本):

    app screenshot

    另外,我也不太清楚到底是什么 vh.itemView ?

    Github issue 2349

    1 回复  |  直到 6 年前
        1
  •  1
  •   mikepenz    6 年前

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

    这个 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
    

    但也可以转到适配器的根目录。获取适配器: 然后从任何适配器更新项目。

    此外,需要注意的是,您可能希望提供 payload 允许优化刷新。(使用自定义负载,然后通过更新视图对该特定负载作出反应)