零基础学鸿蒙编程-UI控件_ListContainer 原创 精华

发布于 2021-11-22 22:16
浏览
0收藏

什么是ListContainer

ListContainer是用于显示列表的UI控件.效果图:
零基础学鸿蒙编程-UI控件_ListContainer-开源基础软件社区

基础用法

  1. 在主布局文件"ability_main.xml"中增加ListContainer
 <ListContainer
        ohos:id="$+id:list_container"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:layout_alignment="center"/>

零基础学鸿蒙编程-UI控件_ListContainer-开源基础软件社区
2. 新增一个Provider用于展示列表中每一行内容

  • 布局文件:list_container_item.xml
<?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:orientation="vertical">

    <Text
        ohos:id="$+id:titleText"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text_size="30vp"
        ohos:layout_alignment="center"/>

    <Component
        ohos:height="1vp"
        ohos:width="match_parent"
        ohos:background_element="#E8E8E8"/>

</DirectionalLayout>

java代码:MyProvider.java

import ohos.agp.components.*;

import java.util.List;

public class MyProvider extends BaseItemProvider {
    private final List<String> dataList;

    public MyProvider(List<String> dataList) {
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList == null ? 0 : dataList.size();
    }

    @Override
    public Object getItem(int i) {
        if (dataList != null && i >= 0 && i < dataList.size()) {
            return dataList.get(i);
        }
        return 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(componentContainer.getContext()).parse(ResourceTable.Layout_list_container_item, null, false);
        } else {
            cpt = component;
        }
        String data = this.dataList.get(i);
        Text titleText = (Text) cpt.findComponentById(ResourceTable.Id_titleText);
        titleText.setText(data);
        return cpt;
    }
}
  1. 在Slice中实例化Provider,设置数据,并将provider设置给ListContainer
public class MainAbilitySlice extends AbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        MyProvider myProvider = new MyProvider(getData());
        listContainer.setItemProvider(myProvider);
    }

    private List<String> getData() {
        List<String> dataList = new ArrayList<>();
        for (int i = 1; i <= 20; i++) {
            dataList.add("数据-" + (i));
        }
        return dataList;
    }
}

完整源代码

https://gitee.com/hspbc/harmonyos_demos/tree/master/listContainerDemo

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