HarmonyOS开发44:ListContainer实现微信主界面

首先修改 itemview.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">


    <Image
        ohos:id="$+id:img"
        ohos:height="match_content"
        ohos:width="match_content"/>

    <DirectionalLayout
        ohos:height="50vp"
        ohos:width="240vp"
        ohos:left_margin="10vp"
        ohos:orientation="vertical">

        <Text
            ohos:id="$+id:title"
            ohos:height="20vp"
            ohos:width="240vp"
            ohos:text="标题"
            ohos:text_size="20fp"
            ohos:top_margin="5vp"
            />

        <Text
            ohos:id="$+id:msg"
            ohos:height="15vp"
            ohos:width="240vp"
            ohos:text="消息"
            ohos:text_size="15fp"
            ohos:top_margin="5vp"
            />
    </DirectionalLayout>

    <Text
        ohos:id="$+id:time"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="00:00"
        ohos:text_size="20fp"/>

</DirectionalLayout>

再修改domain中的Item.java

package com.example.listcontainerapplication2.domain;

public class Item {
    private int img;
    private String title;
    private String message;
    private String time;

    public Item() {
    }

    public Item(int img, String title, String message, String time) {
        this.img = img;
        this.title = title;
        this.message = message;
        this.time = time;
    }

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

同时将以前用到的吐司弹框加到代码当中

修改MainAbilitySlice.java

    //以后这些数据并不是在app中写的,而是服务器中传进来的
    public ArrayList<Item> getData() {
        //应该:访问服务器,获取要展示的数据
        ArrayList<Item> list = new ArrayList<>();
        //给集合添加数据
        for (int i = 0; i < 100; i++) {
            list.add(new Item(ResourceTable.Media_photo, "标题", "item" + i, "00:00"));
        }
        return list;
    }

最后来修改一下ItemProvider.java

package com.example.listcontainerapplication2.provider;

import com.example.listcontainerapplication2.ResourceTable;
import com.example.listcontainerapplication2.domain.Item;
import com.example.listcontainerapplication2.utils.ToastUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.ArrayList;

public class ItemProvider extends BaseItemProvider implements Component.ClickedListener, Component.LongClickedListener {

    private ArrayList<Item> list = new ArrayList<>();
    private AbilitySlice abilitySlice;

    public ItemProvider(ArrayList<Item> list, AbilitySlice abilitySlice) {
        this.list = list;
        this.abilitySlice = abilitySlice;
    }

    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        if (list == null) {
            return null;
        }
        return list.get(i);
    }

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

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {

        if (component == null) {
            component = LayoutScatter.getInstance(abilitySlice).parse(ResourceTable.Layout_itemview, null, false);
        }

        DirectionalLayout dl = (DirectionalLayout) component;

        dl.setClickedListener(this);
        dl.setLongClickedListener(this);

        Item item = list.get(i);
        Image img = (Image) dl.findComponentById(ResourceTable.Id_img);
        Text title = (Text) dl.findComponentById(ResourceTable.Id_title);
        Text msg = (Text) dl.findComponentById(ResourceTable.Id_msg);
        Text time = (Text) dl.findComponentById(ResourceTable.Id_time);

        img.setImageAndDecodeBounds(item.getImg());
        title.setText(item.getTitle());
        msg.setText(item.getMessage());
        time.setText(item.getTime());

        return dl;
    }

    @Override
    public void onClick(Component component) {
        ToastUtils.showDialog(abilitySlice, "跳转聊天界面");
    }

    @Override
    public void onLongClicked(Component component) {
        ToastUtils.showDialog(abilitySlice, "显示设置弹框");
    }
}

实现效果:
请添加图片描述 请添加图片描述请添加图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GeniusAng丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值