鸿蒙 harmonyOs ListContainer notifyDataChanged()更新无效 原创

发布于 2021-9-15 10:05
浏览
0收藏

ListContainer 的使用过程中遇到的问题

ListContainer 在使用Provider时发现 通过构造方法传入的list 数据 = 赋值 是不能通过 notifyDataChanged() 更新数据的

   //例如  documentBeanTitleList
      
titleProvider = new CommonProvider<DocumentBean>(getContext(), ResourceTable.Layout_item_document_flider, documentBeanTitleList) {
          @Override
          protected void convert(ViewHolder viewHolder, DocumentBean documentBean, int i) {
              Text tvDeptName = viewHolder.getComponent(ResourceTable.Id_tv_deptname);
              DirectionalLayout directionalLayout = viewHolder.getComponent(ResourceTable.Id_ll_item);
              Image image = viewHolder.getComponent(ResourceTable.Id_iv_next);
              tvDeptName.setText(documentBean.getName());
          
              if (i + 1 == documentBeanTitleList.size()) {
                  image.setVisibility(Component.INVISIBLE);
                  try {
                      tvDeptName.setTextColor(new Color(getAbility().getResourceManager().getElement(ResourceTable.Color_black).getColor()));
                  } catch (IOException e) {
                      e.printStackTrace();
                  } catch (NotExistException e) {
                      e.printStackTrace();
                  } catch (WrongTypeException e) {
                      e.printStackTrace();
                  }
              } else {
                  image.setVisibility(Component.VISIBLE);
                  try {
                      tvDeptName.setTextColor(new Color(getAbility().getResourceManager().getElement(ResourceTable.Color_blue).getColor()));
                  } catch (IOException e) {
                      e.printStackTrace();
                  } catch (NotExistException e) {
                      e.printStackTrace();
                  } catch (WrongTypeException e) {
                      e.printStackTrace();
                  }
              }
    
                  }
              });
          }
      };

    listContainer.setItemProvider(titleProvider);

      documentBeanTitleList.add(documentBean);
//  添加 删除 界面更新有效果
                                      
      titleProvider.notifyDataChanged();
 //  = 赋值 的方式 界面更新无效果
      List<DocumentBean> list =new ArryList<>();
       list.add(new DocumentBean());
    documentBeanTitleList = list ; 
       titleProvider.notifyDataChanged();
  //   addAll 的方式有效果
      List<DocumentBean> list =new ArryList<>();
       list.add(new DocumentBean());
      
    documentBeanTitleList.addAll(list) ; 
       titleProvider.notifyDataChanged();
       

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2021-9-15 15:07:07修改
3
收藏
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐