0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

鸿蒙版微信聊天UI效果实现!

OpenHarmony技术社区 来源:鸿蒙技术社区 作者:鸿蒙技术社区 2021-11-15 09:35 次阅读

最近开发中要做一个类似微信聊天的工单系统客服中心界面(安卓版)所以想着也模仿一个鸿蒙版(基于 Java UI 的,JS UI 版本的后期更新哈) 那么废话不多数说我们正式开始。

具体实现

mainabiilty 布局文件:

<DependentLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">

<DependentLayout
ohos:id="$+id:company_page_dl"
ohos:height="50vp"
ohos:width="match_parent"
ohos:orientation="horizontal"
ohos:align_parent_bottom="true"
>

<Button
ohos:id="$+id:main_my_btn"
ohos:width="match_content"
ohos:height="match_content"
ohos:text="发送"
ohos:text_size="35vp"
ohos:align_parent_right="true"
ohos:background_element="$graphic:background_btn"
>
Button>
<TextField
ohos:id="$+id:main_textfiled"
ohos:width="match_parent"
ohos:height="match_parent"
ohos:hint="请输入你的消息"
ohos:vertical_center="true"
ohos:text_size="50"
ohos:left_of="$id:main_my_btn"
ohos:layout_alignment="left"
>
TextField>
DependentLayout>

<ListContainer
ohos:above="$id:company_page_dl"
ohos:id="$+id:main_list"
ohos:height="match_parent"
ohos:width="match_parent"
>
ListContainer>

DependentLayout>
观察布局文件,我们可以看到写了一个列表控件 ListContainer 来装载发送出去的消息和接收到的消息。

然后底部写了一个 TextField 控件来处理用户的输入和一个 button 来触发发送的动作。

逻辑代码

我们初始化对应控件并且 listContainer 和适配器绑定到一起:

privatevoidinitview(){
listContainer=(ListContainer)findComponentById(ResourceTable.Id_main_list);
textField=(TextField)findComponentById(ResourceTable.Id_main_textfiled);
mainbtn=(Button)findComponentById(ResourceTable.Id_main_my_btn);
mainbtn.setClickedListener(this);
myProvider=newMyProvider(data,getAbility());
listContainer.setItemProvider(myProvider);
myProvider.notifyDataChanged();//有新消息时,刷新ListView中的显示
}

①初始默认假数据

我们方便展示就写了 3 条假数据仅供展示:

privatevoidinitMsg(){
Msgmsg1=newMsg("你好",Msg.RECEIVED);
data.add(msg1);
Msgmsg2=newMsg("你好呀",Msg.SENT);
data.add(msg2);
Msgmsg3=newMsg("很高兴认识你",Msg.RECEIVED);
data.add(msg3);
}

②用户输入逻辑:
@Override
publicvoidonClick(Componentcomponent){
content=textField.getText().toString();
switch(component.getId()){
caseResourceTable.Id_main_my_btn:
if(!flag){
Msgmsg=newMsg(content,Msg.SENT);
data.add(msg);
flag=true;
}else{
Msgmsg=newMsg(content,Msg.RECEIVED);
data.add(msg);
flag=false;
}
myProvider.notifyDataChanged();//有新消息时,刷新ListView中的显示
textField.setText("");//清空输入框的内容
break;

default:
break;

}

}
我们通过一个布尔值 flag 来做一个开关处理用户输入的,动作轮流来处理是接收到消息还是发送出消息。

发送消息:

Msgmsg=newMsg(content,Msg.SENT);
data.add(msg);

接收消息:

Msgmsg=newMsg(content,Msg.RECEIVED);
data.add(msg);

bena 类

packagecom.example.imdemo.bean;

publicclassMsg{

publicstaticfinalintRECEIVED=0;//收到一条消息

publicstaticfinalintSENT=1;//发出一条消息

privateStringcontent;//消息的内容

privateinttype;//消息的类型

publicMsg(Stringcontent,inttype){
this.content=content;
this.type=type;
}

publicStringgetContent(){
returncontent;
}

publicintgetType(){
returntype;
}
}
我们分别定义了 2 个常量和 2 个变量来处理我们的消息逻辑。

适配器

适配器 item.xml 布局:


<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_content"
ohos:width="match_parent"
ohos:orientation="vertical">
<DirectionalLayout
ohos:id="$+id:left_layout"
ohos:height="match_content"
ohos:width="match_content"
ohos:layout_alignment="left"
ohos:background_element="$graphic:background_blue"
ohos:left_margin="5vp"
ohos:visibility="visible"
ohos:top_margin="10vp"
>

<Text
ohos:id="$+id:left_msg"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="哈哈哈测试"
ohos:text_color="#fff"
ohos:text_size="20vp"
ohos:margin="10vp"
>
Text>

DirectionalLayout>



<DirectionalLayout
ohos:id="$+id:right_Layout"
ohos:height="match_content"
ohos:width="match_content"
ohos:layout_alignment="right"
ohos:background_element="$graphic:background_red"
ohos:right_margin="5vp"
ohos:visibility="visible"
>
<Text
ohos:id="$+id:right_msg"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="哈哈哈测试"
ohos:text_color="#fff"
ohos:text_size="20vp"
ohos:margin="10vp"
>
Text>
DirectionalLayout>
DirectionalLayout>

item 布局预览效果:

19f2e066-458d-11ec-b939-dac502259ad0.png适配器逻辑代码:
packagecom.example.imdemo.provider;
importcom.example.imdemo.ResourceTable;
importcom.example.imdemo.bean.Msg;
importohos.aafwk.ability.Ability;
importohos.agp.components.*;

importjava.util.List;

publicclassMyProviderextendsBaseItemProvider{

privateListlist;
privateAbilityability;


publicMyProvider(Listlist,Abilityability){
this.list=list;
this.ability=ability;
}

@Override
publicintgetCount(){
returnlist.size();
}

@Override
publicObjectgetItem(inti){
returnlist.get(i);
}

@Override
publiclonggetItemId(inti){
returni;
}

@Override
publicComponentgetComponent(inti,Componentcomponent,ComponentContainercomponentContainer){

ViewHodlerhodler=null;
Msgmsg=list.get(i);
if(component==null){
component=LayoutScatter.getInstance(ability).parse(ResourceTable.Layout_item,null,false);
hodler=newViewHodler();
hodler.leftLayout=(DirectionalLayout)component.findComponentById(ResourceTable.Id_left_layout);
hodler.rightLayout=(DirectionalLayout)component.findComponentById(ResourceTable.Id_right_Layout);
hodler.leftMsg=(Text)component.findComponentById(ResourceTable.Id_left_msg);
hodler.rightMsg=(Text)component.findComponentById(ResourceTable.Id_right_msg);
component.setTag(hodler);
}else{
hodler=(ViewHodler)component.getTag();
}
System.out.println("type--->"+msg.getType());
if(msg.getType()==Msg.RECEIVED){
System.out.println("左边消息");
//如果是收到的消息,则显示左边消息布局,将右边消息布局隐藏
hodler.leftLayout.setVisibility(0);
hodler.rightLayout.setVisibility(1);
hodler.leftMsg.setText(msg.getContent());
}elseif(msg.getType()==Msg.SENT){
System.out.println("右边消息");
//如果是发出去的消息,显示右边布局的消息布局,将左边的消息布局隐藏
hodler.rightLayout.setVisibility(0);
hodler.leftLayout.setVisibility(1);
hodler.rightMsg.setText(msg.getContent());
}
returncomponent;
}

classViewHodler{
DirectionalLayoutleftLayout;
DirectionalLayoutrightLayout;
TextleftMsg;
TextrightMsg;

}
}
我们通过在 getComponent 方法中通过小标 i 来遍历集合然后拿到里面每一个对应里面的 type 属性来判断是显示左边布局还是右边布局。 也就是对应的发送消息和接收消息的 UI,我们通过简单布局显示影藏来实现消息的左右两边显示效果,到此整个仿微信聊天的布局 UI 效果就讲完了 。

总结

鸿蒙的仿微信聊天 UI 效果实现起来相对比较简单,其实还有一种办法那就是 ListContainer 的多布局也是通过 bean 里面的标识来显示左右不同的布局实现聊天界面的效果。

因为篇幅有限这里就不展开讲了有兴趣的同学可以私下研究。最后希望我的文章能帮助到各位解决问题,以后我还会贡献更多有用的代码分享给大家。

项目地址:

https://gitee.com/qiuyu123/hms_im_demo
编辑:jq

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • JAVA
    +关注

    关注

    19

    文章

    2904

    浏览量

    103041
  • JS
    JS
    +关注

    关注

    0

    文章

    73

    浏览量

    17859
  • ui
    ui
    +关注

    关注

    0

    文章

    198

    浏览量

    21205
  • 鸿蒙
    +关注

    关注

    55

    文章

    1677

    浏览量

    42140

原文标题:鸿蒙版微信聊天UI效果实现!

文章出处:【微信号:gh_834c4b3d87fe,微信公众号:OpenHarmony技术社区】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    最新开源代码证实!“鸿蒙原生版”正在积极开发中

    半年来,许多国产 APP 都相继推出“鸿蒙原生版”,但却始终没看见国民级应用——的身影。 对此,坊间传言称华为鸿蒙
    发表于 05-08 17:08

    实锤!腾讯终于拥抱鸿蒙生态,鸿蒙原生版本即将上线

    大家都知道, 目前已知纯血鸿蒙星河版next将于今年6月份开启Bate版本的测试 ,也就是说原生鸿蒙系统快上线了。 而目前对于鸿蒙生态的发展,大家最关心的恐怕只有腾讯系的
    发表于 04-30 21:14

    腾讯突然宣布,鸿蒙版要来了!

    」的身影,不少网友更是喊话腾讯,希望QQ、尽快适配。 现在, 好消息来了! 最近在网上看到一条重磅消息—— 原生鸿蒙版「
    发表于 04-30 19:34

    鸿蒙实战开发ArkTS运用:【ai聊天框】

    用一个ArkTS编写的HarmonyOS原生聊天UI框架,提供了开箱即用的聊天对话组件。
    的头像 发表于 03-08 15:38 287次阅读
    <b class='flag-5'>鸿蒙</b>实战开发ArkTS运用:【ai<b class='flag-5'>聊天</b>框】

    鸿蒙实战项目开发:【短信服务】

    、Java、前端等等开发人员,想要转入鸿蒙方向发展。可以直接领取这份资料辅助你的学习。下面是鸿蒙开发的学习路线图。 ! 高清完整版请前往→[+mau123789是喔!]《鸿蒙NEXT
    发表于 03-03 21:29

    鸿蒙OS之UI架构解析

    agconnect的依赖,Previewer编译失败。 我们可以看到Index和数据获取的逻辑强耦合在了一起。没有专注于他自身的UI布局的功能。 数据请求扔给另一个类IndexViewModel 那一
    发表于 02-20 15:36

    鸿蒙开发-HarmonyOS UI架构

    报错,说因为有agconnect的依赖,Previewer编译失败。 我们可以看到Index和数据获取的逻辑强耦合在了一起。没有专注于他自身的UI布局的功能。 数据请求扔给另一个类
    发表于 02-16 16:38

    鸿蒙实战开发-全局UI方法的功能

    使用全局UI的方法定义日期滑动选择器弹窗并弹出。
    的头像 发表于 02-02 17:13 208次阅读
    <b class='flag-5'>鸿蒙</b>实战开发-全局<b class='flag-5'>UI</b>方法的功能

    基于Crazyflie和TOF传感器的自旋停效果实现

    今天小编给大家带来的是新加坡的Maker Chathuranga Liyanage使用TOF传感器让Crazyflie实现高度自保持的项目。
    的头像 发表于 01-25 18:20 1526次阅读
    基于Crazyflie和TOF传感器的自旋停<b class='flag-5'>效果实现</b>

    HarmonyOS SDK,助力开发者打造焕然一新的鸿蒙原生应用

    六大领域的开发能力,为开发者带来简洁、高效的开发体验,开发者只需通过 API 调用即可实现丰富的鸿蒙原生应用功能和独特体验。同时,在开发效率上,HarmonyOS SDK 更进一步,通过整合开发高频
    发表于 01-19 10:31

    免费学习鸿蒙(HarmonyOS)开发,一些地址分享

    。 分别有ArkTS语言、ArkUI声明式UI开发、Stage模型、北向和南向的开发等等鸿蒙入门到实战的内容。
    发表于 01-12 20:48

    华为鸿蒙系统

    华为鸿蒙系统(HUAWEI Harmony OS),是华为公司在2019年8月9日于东莞举行的华为开发者大会(HDC.2019)上正式发布的操作系统。 华为鸿蒙系统是一款全新的面向全场景的分布式
    发表于 11-02 19:39

    鸿蒙 OS 应用开发初体验

    什么是 HarmonyOS? HarmonyOS(鸿蒙操作系统)是华为公司开发的一款基于内核的分布式操作系统。它是一个面向物联网(IoT)时代的全场景操作系统,旨在为各种类型的设备提供统一
    发表于 11-02 19:38

    鸿蒙应用ui布局

    请问,在用java开发鸿蒙应用布局UI时,怎么才能全屏布局(不显示labelb标题)
    发表于 09-20 22:09

    使用TouchGFX MVP来实现UI和板子外设双向通信

    页面效果和界面切换可以在VS中进行仿真,然后再在IAR/KEIL中加入控制硬件的程序 在lAR中控制MCu的程序可以和UI部分在Model层中用宏定义分离
    发表于 09-11 06:16