鸿蒙从0搭建Chatgpt App客户端,第二篇之聊天页面搭建

上一篇我们讲到自定义ViewChatgpt左侧的头像,本节继续讲解三个部分

  1. 右侧内容区域
  2. 整个item的布局
  3. 整个列表数据的加载

先看看右侧内容

先上代码上图,可以看出此视图用了Column+Text

js 复制代码
@Entry
@Component
export default struct ContentComponent {
  build() {
    Column() {
      Text("PATRICIA PERCY")
        .fontSize(10)
        .margin({ bottom: 6 })
      Text("鸿蒙开发怎么样")
        .fontSize(14)
    }.alignItems(HorizontalAlign.Start)
    .padding({ top: 16, left: 8, right: 16, bottom: 8 })
  }
}

Text控件上节我们已经介绍过,下面介绍下Column布局 Column其实是线性布局的一种,类似于android中的LinearLayout,子元素垂直方向上排列

Column可以让子元素在水平方向上有三种排列方式

HorizontalAlign.Start, HorizontalAlign.Center,HorizontalAlign.End

回到我们的项目,这里用到了HorizontalAlign.Start,让子元素靠左剧中,让用户名跟内容靠左

下面我们将昨天绘制的头像区域跟内容区域结合起来,这里为了给大家看清楚布局区域,特意给指定区域添加背景色

这里用到一个Row布局将整个item分成两部分,左边放头像占比10%,右侧放内容区域占比90%,下面我们讲下Row布局,Row布局也是线性布局,相当于让子元素横向排列

跟Column布局一样Row布局也有三种排列方式VerticalAlign.Top,VerticalAlign.Center,VerticalAlign.Bottom分别对应上中下,本项目设置的是VerticalAlign.Top

item写完后下面就介绍下列表布局老规矩先上图

js 复制代码
@Entry
@Component
export default struct ChatList {
  @Provide chatListData: ListDataSource = new ListDataSource();

  build() {
    Row() {
      List({ space: 8 }) {
        LazyForEach(this.chatListData, (item: ChatListItemType) => {
          ListItem() {
            Row() {
              Column() {
                Header()
                  .width(24)
                  .height(24).margin({ top: 8 })
              }.width('10%')
              .height(40)
              .alignItems(HorizontalAlign.Center)
              Column() {
                Text(item.userName)
                  .fontSize(10)
                  .margin({ bottom: 6 })
                Text(item.content)
                  .fontSize(14)
              }
              .alignItems(HorizontalAlign.Start)
              .padding({ top: 8, left: 8, right: 16, bottom: 8 })
              .width('90%')
            }
            .width(commonConst.LAYOUT_WIDTH_OR_HEIGHT)
            .alignItems(VerticalAlign.Top)
          }
        })
      }
      .width(commonConst.GOODS_LIST_WIDTH)
    }
    .justifyContent(FlexAlign.Center)
    .width(commonConst.LAYOUT_WIDTH_OR_HEIGHT)
  }
}

可以看出其实就是list控件包裹了我们上面实现的item,下面介绍下list用法看下最简单用法 list也有水平跟竖直排列

默认是垂直方向

js 复制代码
@Component
struct PlatList {
  build() {
    List() {
      ListItem() {
        Text('安卓').fontSize(24)
      }

      ListItem() {
        Text('苹果').fontSize(24)
      }

      ListItem() {
        Text('鸿蒙').fontSize(24)
      }
    }
    .backgroundColor('#FFF1F3F5')
    .alignListItem(ListItemAlign.Center)
  }
}

横向排列

多列可以设置列数

js 复制代码
List() {
  ...
}
.lanes(2)

接下来我们看下数据绑定 ArkTS通过ForEach提供了组件的循环渲染能力 首先我们定义一个class对象

js 复制代码
export class ChatListItemType {
  headImg: Resource;
  userName: Resource;
  content: Resource;

  constructor(headImg: Resource, userName: Resource,content: Resource) {
    this.headImg = headImg;
    this.userName = userName;
    this.content = content
  }
}

接下来模拟一组数据

js 复制代码
export const chatInitialList: ChatListItemType[] = [
  new ChatListItemType($r('app.media.goodsImg'), $r('app.string.user'), $r('app.string.user_qa')),
  new ChatListItemType($r('app.media.goodsImg_2'), $r('app.string.gpt'), $r('app.string.gpt_answer')),
]

然后就可以在我们ChatList做列表迭代

最后上个图

下一节预告,我们将介绍头部区域,底部区域实现,欢迎学习

相关推荐
菠萝加点糖1 小时前
Kotlin Data包含ByteArray类型
android·开发语言·kotlin
不凡的凡4 小时前
鸿蒙图片相似性对比
华为·harmonyos
Georgewu6 小时前
【HarmonyOS】HAR和HSP循环依赖和依赖传递问题详解
harmonyos
Georgewu9 天前
【HarmonyOS 5】鸿蒙跨平台开发方案详解(一)
flutter·harmonyos
IAM四十二9 天前
Google 端侧 AI 框架 LiteRT 初探
android·深度学习·tensorflow
CYRUS_STUDIO9 天前
手把手教你用 Chrome 断点调试 Frida 脚本,JS 调试不再是黑盒
android·app·逆向
Just丶Single9 天前
安卓NDK初识
android
万少10 天前
重磅推出 🔥 HarmonyOS AI 助手 CodeGenie V6 的使用教程
前端·harmonyos
我睡醒再说10 天前
纯血HarmonyOS5 打造小游戏实践:绘画板(附源文件)
harmonyos
我睡醒再说10 天前
HarmonyOS 5 ArkTS Worker线程:构建高性能移动应用的并行计算引擎
harmonyos