0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

鴻蒙版微信聊天UI效果實(shí)現(xiàn)!

OpenHarmony技術(shù)社區(qū) ? 來源:鴻蒙技術(shù)社區(qū) ? 作者:鴻蒙技術(shù)社區(qū) ? 2021-11-15 09:35 ? 次閱讀

最近開發(fā)中要做一個(gè)類似微信聊天的工單系統(tǒng)客服中心界面(安卓版)所以想著也模仿一個(gè)鴻蒙版(基于 Java UI 的,JS UI 版本的后期更新哈) 那么廢話不多數(shù)說我們正式開始。

具體實(shí)現(xiàn)

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="發(fā)送"
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="請(qǐng)輸入你的消息"
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>
觀察布局文件,我們可以看到寫了一個(gè)列表控件 ListContainer 來裝載發(fā)送出去的消息和接收到的消息。

然后底部寫了一個(gè) TextField 控件來處理用戶的輸入和一個(gè) button 來觸發(fā)發(fā)送的動(dòng)作。

邏輯代碼

我們初始化對(duì)應(yīng)控件并且 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();//有新消息時(shí),刷新ListView中的顯示
}

①初始默認(rèn)假數(shù)據(jù)

我們方便展示就寫了 3 條假數(shù)據(jù)僅供展示:

privatevoidinitMsg(){
Msgmsg1=newMsg("你好",Msg.RECEIVED);
data.add(msg1);
Msgmsg2=newMsg("你好呀",Msg.SENT);
data.add(msg2);
Msgmsg3=newMsg("很高興認(rèn)識(shí)你",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();//有新消息時(shí),刷新ListView中的顯示
textField.setText("");//清空輸入框的內(nèi)容
break;

default:
break;

}

}
我們通過一個(gè)布爾值 flag 來做一個(gè)開關(guān)處理用戶輸入的,動(dòng)作輪流來處理是接收到消息還是發(fā)送出消息。

發(fā)送消息:

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;//發(fā)出一條消息

privateStringcontent;//消息的內(nèi)容

privateinttype;//消息的類型

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

publicStringgetContent(){
returncontent;
}

publicintgetType(){
returntype;
}
}
我們分別定義了 2 個(gè)常量和 2 個(gè)變量來處理我們的消息邏輯。

適配器

適配器 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="哈哈哈測(cè)試"
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="哈哈哈測(cè)試"
ohos:text_color="#fff"
ohos:text_size="20vp"
ohos:margin="10vp"
>
Text>
DirectionalLayout>
DirectionalLayout>

item 布局預(yù)覽效果:

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("右邊消息");
//如果是發(fā)出去的消息,顯示右邊布局的消息布局,將左邊的消息布局隱藏
hodler.rightLayout.setVisibility(0);
hodler.leftLayout.setVisibility(1);
hodler.rightMsg.setText(msg.getContent());
}
returncomponent;
}

classViewHodler{
DirectionalLayoutleftLayout;
DirectionalLayoutrightLayout;
TextleftMsg;
TextrightMsg;

}
}
我們通過在 getComponent 方法中通過小標(biāo) i 來遍歷集合然后拿到里面每一個(gè)對(duì)應(yīng)里面的 type 屬性來判斷是顯示左邊布局還是右邊布局。 也就是對(duì)應(yīng)的發(fā)送消息和接收消息的 UI,我們通過簡(jiǎn)單布局顯示影藏來實(shí)現(xiàn)消息的左右兩邊顯示效果,到此整個(gè)仿微信聊天的布局 UI 效果就講完了 。

總結(jié)

鴻蒙的仿微信聊天 UI 效果實(shí)現(xiàn)起來相對(duì)比較簡(jiǎn)單,其實(shí)還有一種辦法那就是 ListContainer 的多布局也是通過 bean 里面的標(biāo)識(shí)來顯示左右不同的布局實(shí)現(xiàn)聊天界面的效果。

因?yàn)槠邢捱@里就不展開講了有興趣的同學(xué)可以私下研究。最后希望我的文章能幫助到各位解決問題,以后我還會(huì)貢獻(xiàn)更多有用的代碼分享給大家。

項(xiàng)目地址:

https://gitee.com/qiuyu123/hms_im_demo
編輯:jq

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • JAVA
    +關(guān)注

    關(guān)注

    19

    文章

    2943

    瀏覽量

    104101
  • JS
    JS
    +關(guān)注

    關(guān)注

    0

    文章

    75

    瀏覽量

    17983
  • ui
    ui
    +關(guān)注

    關(guān)注

    0

    文章

    202

    瀏覽量

    21290
  • 鴻蒙
    +關(guān)注

    關(guān)注

    56

    文章

    2267

    瀏覽量

    42489

原文標(biāo)題:鴻蒙版微信聊天UI效果實(shí)現(xiàn)!

文章出處:【微信號(hào):gh_834c4b3d87fe,微信公眾號(hào):OpenHarmony技術(shù)社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

收藏 人收藏

    評(píng)論

    相關(guān)推薦

    VA段碼COG液晶屏可個(gè)性化靈活設(shè)計(jì)UI實(shí)現(xiàn)創(chuàng)意UI的高對(duì)比高亮多色彩炫酷顯示。

    VA段碼COG液晶屏可個(gè)性化靈活設(shè)計(jì)UI實(shí)現(xiàn)創(chuàng)意UI的高對(duì)比高亮多色彩炫酷顯示。
    的頭像 發(fā)表于 07-11 10:50 ?318次閱讀
    VA段碼COG液晶屏可個(gè)性化靈活設(shè)計(jì)<b class='flag-5'>UI</b>,<b class='flag-5'>實(shí)現(xiàn)</b>創(chuàng)意<b class='flag-5'>UI</b>的高對(duì)比高亮多色彩炫酷顯示。

    深耕鴻蒙生態(tài),國(guó)科旗艦芯片獲“鴻蒙4.0”首款認(rèn)證

    7月9日,國(guó)科宣布旗下超高清視頻解碼及商顯芯片通過OpenHarmony4.0版本兼容性測(cè)評(píng),獲頒鴻蒙生態(tài)產(chǎn)品兼容性證書。其中,國(guó)科GK6323V100C是業(yè)界首款通過鴻蒙4.0兼
    的頭像 發(fā)表于 07-10 13:10 ?236次閱讀
    深耕<b class='flag-5'>鴻蒙</b>生態(tài),國(guó)科<b class='flag-5'>微</b>旗艦芯片獲“<b class='flag-5'>鴻蒙</b>4.0”首款認(rèn)證

    鴻蒙Ability Kit(程序框架服務(wù))【UIAbility組件與UI的數(shù)據(jù)同步】

    基于當(dāng)前的應(yīng)用模型,可以通過以下幾種方式來實(shí)現(xiàn)UIAbility組件與UI之間的數(shù)據(jù)同步。
    的頭像 發(fā)表于 06-03 10:26 ?349次閱讀
    <b class='flag-5'>鴻蒙</b>Ability Kit(程序框架服務(wù))【UIAbility組件與<b class='flag-5'>UI</b>的數(shù)據(jù)同步】

    鴻蒙ArkUI:【從代碼到UI顯示的整體渲染流程】

    方舟開發(fā)框架(簡(jiǎn)稱ArkUI)是鴻蒙開發(fā)的UI框架,提供如下兩種開發(fā)范式,我們 **只學(xué)聲明式開發(fā)范式**
    的頭像 發(fā)表于 05-13 16:06 ?594次閱讀
    <b class='flag-5'>鴻蒙</b>ArkUI:【從代碼到<b class='flag-5'>UI</b>顯示的整體渲染流程】

    最新開源代碼證實(shí)!“鴻蒙原生版”正在積極開發(fā)中

    半年來,許多國(guó)產(chǎn) APP 都相繼推出“鴻蒙原生版”,但卻始終沒看見國(guó)民級(jí)應(yīng)用——的身影。 對(duì)此,坊間傳言稱華為鴻蒙
    發(fā)表于 05-08 17:08

    實(shí)錘!騰訊終于擁抱鴻蒙生態(tài),鴻蒙原生版本即將上線

    大家都知道, 目前已知純血鴻蒙星河版next將于今年6月份開啟Bate版本的測(cè)試 ,也就是說原生鴻蒙系統(tǒng)快上線了。 而目前對(duì)于鴻蒙生態(tài)的發(fā)展,大家最關(guān)心的恐怕只有騰訊系的
    發(fā)表于 04-30 21:14

    騰訊突然宣布,鴻蒙版要來了!

    」的身影,不少網(wǎng)友更是喊話騰訊,希望QQ、盡快適配。 現(xiàn)在, 好消息來了! 最近在網(wǎng)上看到一條重磅消息—— 原生鴻蒙版「
    發(fā)表于 04-30 19:34

    鴻蒙實(shí)戰(zhàn)開發(fā)ArkTS運(yùn)用:【ai聊天框】

    用一個(gè)ArkTS編寫的HarmonyOS原生聊天UI框架,提供了開箱即用的聊天對(duì)話組件。
    的頭像 發(fā)表于 03-08 15:38 ?668次閱讀
    <b class='flag-5'>鴻蒙</b>實(shí)戰(zhàn)開發(fā)ArkTS運(yùn)用:【ai<b class='flag-5'>聊天</b>框】

    【JAVA UI】【HarmonyOS】【Demo】 鴻蒙如何進(jìn)行 xml 解析

    鴻蒙鴻蒙如何進(jìn)行數(shù)據(jù)解析 【問題描述】有時(shí)候我們從服務(wù)器獲取是 xml 格式數(shù)據(jù),我們需要將 xml 轉(zhuǎn)化成 model 對(duì)象,該如何使用呢?下面舉個(gè)例子說明一下,將分以下幾步進(jìn)行 1.準(zhǔn)備條件
    的頭像 發(fā)表于 02-19 15:59 ?383次閱讀
    【JAVA <b class='flag-5'>UI</b>】【HarmonyOS】【Demo】 <b class='flag-5'>鴻蒙</b>如何進(jìn)行 xml 解析

    鴻蒙開發(fā)-HarmonyOS UI架構(gòu)

    報(bào)錯(cuò),說因?yàn)橛衋gconnect的依賴,Previewer編譯失敗。 我們可以看到Index和數(shù)據(jù)獲取的邏輯強(qiáng)耦合在了一起。沒有專注于他自身的UI布局的功能。 數(shù)據(jù)請(qǐng)求扔給另一個(gè)類
    發(fā)表于 02-16 16:38

    鴻蒙實(shí)戰(zhàn)開發(fā)-全局UI方法的功能

    使用全局UI的方法定義日期滑動(dòng)選擇器彈窗并彈出。
    的頭像 發(fā)表于 02-02 17:13 ?429次閱讀
    <b class='flag-5'>鴻蒙</b>實(shí)戰(zhàn)開發(fā)-全局<b class='flag-5'>UI</b>方法的功能

    基于Crazyflie和TOF傳感器的自旋停效果實(shí)現(xiàn)

    今天小編給大家?guī)淼氖切录悠碌腗aker Chathuranga Liyanage使用TOF傳感器讓Crazyflie實(shí)現(xiàn)高度自保持的項(xiàng)目。
    的頭像 發(fā)表于 01-25 18:20 ?2374次閱讀
    基于Crazyflie和TOF傳感器的自旋停<b class='flag-5'>效果實(shí)現(xiàn)</b>

    HarmonyOS SDK,助力開發(fā)者打造煥然一新的鴻蒙原生應(yīng)用

    六大領(lǐng)域的開發(fā)能力,為開發(fā)者帶來簡(jiǎn)潔、高效的開發(fā)體驗(yàn),開發(fā)者只需通過 API 調(diào)用即可實(shí)現(xiàn)豐富的鴻蒙原生應(yīng)用功能和獨(dú)特體驗(yàn)。同時(shí),在開發(fā)效率上,HarmonyOS SDK 更進(jìn)一步,通過整合開發(fā)高頻
    發(fā)表于 01-19 10:31

    華為鴻蒙系統(tǒng)

    華為鴻蒙系統(tǒng)(HUAWEI Harmony OS),是華為公司在2019年8月9日于東莞舉行的華為開發(fā)者大會(huì)(HDC.2019)上正式發(fā)布的操作系統(tǒng)。 華為鴻蒙系統(tǒng)是一款全新的面向全場(chǎng)景的分布式
    發(fā)表于 11-02 19:39

    鴻蒙 OS 應(yīng)用開發(fā)初體驗(yàn)

    什么是 HarmonyOS? HarmonyOS(鴻蒙操作系統(tǒng))是華為公司開發(fā)的一款基于內(nèi)核的分布式操作系統(tǒng)。它是一個(gè)面向物聯(lián)網(wǎng)(IoT)時(shí)代的全場(chǎng)景操作系統(tǒng),旨在為各種類型的設(shè)備提供統(tǒng)一
    發(fā)表于 11-02 19:38