鸿蒙ListContainer多列数据展示实现

创建模拟的数据
List getSongs(){
HiLog.error(LABEL_LOG,“getSongs==”);
List list = new ArrayList();
List list2 = new ArrayList();
List list3 = new ArrayList();
for (int i=0; i<100; i++) {
list.add(new SongData(“歌曲名称:”+i,“歌手:”+i));
}
int i=0;
for (Object data:list) {
if (i=2){//i为需要的列数
i=0;
list3.add(new NewSongModel(list2));
list2=new ArrayList<>();
}
list2.add((SongData) data);
i++;
}
list3.add(new NewSongModel(list2));
HiLog.error(LABEL_LOG,“list3==”+list3.size());
return list3;
}

NewSongModel 类:
public class NewSongModel {
private List songModels;
public NewSongModel(List songModels) {
this.songModels = songModels;
}
public List getSongModels() {
return songModels;
}
public void setSongModels(List songModels) {
this.songModels = songModels;
}
}

Provider类的实现
public class MultSongProvider extends RecycleItemProvider {
private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, “Demo”);

private List<NewSongModel> datas=new ArrayList<>();
private AbilitySlice mSlice;
LayoutScatter layoutScatter;
public MultSongProvider(AbilitySlice mSlice, Context context) {
    this.mSlice = mSlice;
    this.layoutScatter = LayoutScatter.getInstance(context);
}

public void setDataMos(List<NewSongModel> cityMos){
    this.datas.clear();
    this.datas.addAll(cityMos);
    HiLog.error(LABEL_LOG,"datas=="+datas.size());
    notifyDataChanged();

}

@Override
public int getCount() {
    return datas.size();
}

@Override
public Object getItem(int i) {
    return datas.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}
private void onItemClick(UserModel cityMo,int position){
}
@Override
public Component getComponent(int pos, Component component, ComponentContainer componentContainer) {
   /* Component component1= LayoutScatter.getInstance(mSlice).parse(ResourceTable.Layout_layout_user_item,null,false);
    User mo=dataMos.get(i);
    Text text= (Text) component1.findComponentById(ResourceTable.Id_txt_user_item);
    text.setText("姓名:"+mo.getName()+"  年龄:"+mo.getAge()+"  phone:"+mo.getPhone());
    return component1;*/
    Component component1= LayoutScatter.getInstance(mSlice).parse(ResourceTable.Layout_songitem_layout,null,false);
    if (!(component1 instanceof ComponentContainer)){
        return null;
    }
    ComponentContainer container= (ComponentContainer) component1;
    //component相当于Android中的view,其他的和Android中ListView的适配器adapter差不多。
    // 名字区别也不大,不过Android中ListView基本被淘汰了。
    NewSongModel usersModel=datas.get(pos);
    for (int j = 0; j < usersModel.getSongModels().size(); j++) {
        SongData songModel=usersModel.getSongModels().get(j);
        DirectionalLayout directionalLayout= (DirectionalLayout)      LayoutScatter.getInstance(mSlice).parse(ResourceTable.Layout_mulitsong_item,null,false);
        Text songName= (Text) directionalLayout.findComponentById(ResourceTable.Id_songName);
        Text songer= (Text) directionalLayout.findComponentById(ResourceTable.Id_songer);
        songName.setText(songModel.getName());
        songer.setText(songModel.getSinger());
        DirectionalLayout.LayoutConfig buttonLayoutConfig = new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT, DirectionalLayout.HORIZONTAL, 1);//设置布局的权重值,使添加进来的布局均分
        directionalLayout.setPaddingBottom(20);
        directionalLayout.setPaddingTop(20);
        directionalLayout.setLayoutConfig(buttonLayoutConfig);

        container.addComponent(directionalLayout);
    }
    return component1;
}
public interface onDeleteListener{
    void onItemClick(UserModel userBean, int position);
}

}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值