当前位置:   article > 正文

HarmonyOS应用开发基础——ListContainer组件案例详解_harmonyos listcontainer组件案例

harmonyos listcontainer组件案例

HarmonyOS应用开发基础——ListContainer组件案例详解

案例详解

基础代码使用

实现步骤
  1. 给item去指定一个布局xml文件
  2. 书写一个javabean类表示item
  3. 写一个适配器类去管理item
  4. 将适配器交给ListContainer
项目结构

在这里插入图片描述

子界面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:alignment="center" 
	ohos:orientation="vertical"> 
  <ListContainer
	ohos:id="$+id:listcontainer" 
	ohos:height="match_parent"
	ohos:width="match_parent" 			
	ohos:layout_alignment="horizontal_center"/> 
</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
item的xml文件
<?xml version="1.0" encoding="utf-8"?> 
<DirectionalLayout 
	xmlns:ohos="http://schemas.huawei.com/res/ohos" 
	ohos:height="match_content"
 	ohos:width="match_content" 
 	ohos:orientation="horizontal">
  <Text
  ohos:id="$+id:text" 
  ohos:height="match_content" 
  ohos:width="match_content" 
  ohos:text="00:00"
  ohos:text_size="20fp"/>
</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
javaBean文件
public class Item {
    //记录的值就是赋值给item里面的text
private String text;
public Item() {
    }
public Item(String text) {
    this.text = text; }
public String getText() {
    return text; }
public void setText(String text) {
    this.text = text; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
适配器类
package com.example.listcontainerapplication.provider;

import com.example.listcontainerapplication.ResourceTable;
import com.example.listcontainerapplication.domin.Item;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.ArrayList;

public class ItemProvider extends BaseItemProvider {
   
    private ArrayList<Item> list;
    private AbilitySlice as;

    public ArrayList<Item> getList() {
   
        return list;
    }

    public void setList(ArrayList<Item> list) {
   
        this.list = list;
    }

    public AbilitySlice getAs() {
   
        return as;
    }

    public void setAs(AbilitySlice as) {
   
        this.as = as;
    }

    public ItemProvider(ArrayList
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/282130?site
推荐阅读
相关标签
  

闽ICP备14008679号