HarmonyOS ListContainer基础用法

package com.example.myapplication3.Data;

public class City {
    private int Id;
    private String Name;

    public City() {
    }

    public City(int id, String name) {
        Id = id;
        Name = name;
    }

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    @Override
    public String toString() {
        return "City{" +
                "Id=" + Id +
                ", Name='" + Name + '\'' +
                '}';
    }
}
package com.example.myapplication3.slice;

import com.example.myapplication3.Data.City;
import com.example.myapplication3.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySliceListContainer extends AbilitySlice {
    private List<City> data = new ArrayList<City>();

    private void InitData() {
        for (int i = 0; i < 50; i++) {
            data.add(new City(i, "江西" + i));
        }
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main_listcontainer);

        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_listcontainer1);
        InitData();//获取数据
        MainAbilitySliceListContainer container = this;

        listContainer.setItemProvider(new BaseItemProvider() {

            @Override
            public int getCount() {

                return data != null ? data.size() : 0;
            }

            @Override
            public Object getItem(int i) {
                return data != null ? data.get(i) : null;
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
                final Component cpt;
                if (component == null) {
                    cpt = LayoutScatter.getInstance(container)
                            .parse(ResourceTable.Layout_ability_main_listitem, null, false);
                } else {
                    cpt = component;
                }

                Text id = (Text)cpt.findComponentById(ResourceTable.Id_text_id);
                Text name = cpt.findComponentById(ResourceTable.Id_text_name);

                id.setText(String.valueOf(data.get(i).getId()));
                name.setText(data.get(i).getName());

                return cpt;
            }
        });
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="center"
    ohos:orientation="vertical">

   <ListContainer
       ohos:id="$+id:listcontainer1"
       ohos:height="match_content"
       ohos:layout_alignment="center"
       ohos:width="match_parent">
   </ListContainer>

</DirectionalLayout>
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="center"
    ohos:orientation="horizontal">


   <Text
       ohos:id="$+id:text_id"
       ohos:height="match_content"
       ohos:width="match_content"
       ohos:text=""
       ohos:text_size="40vp"
       />
   <Text
       ohos:id="$+id:text_name"
       ohos:height="match_content"
       ohos:width="match_content"
       ohos:text=""
       ohos:text_size="40vp"
       />
</DirectionalLayout>

 1.类:City  |   MainAbilitySliceListContainer

2.xml文件:ability_main_listcontainer  |  ability_main_listitem

HarmonyOS  ListContainer基础用法

 

上一篇:23种设计模式(三)抽象工厂模式


下一篇:18、重载(Overload)和重写(Override)有什么区别?