最近開發(fā)中要做一個類似微信聊天的工單系統(tǒng)客服中心界面(安卓版)所以想著也模仿一個鴻蒙版(基于 Java UI 的,JS UI 版本的后期更新哈) 那么廢話不多數(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="請輸入你的消息"
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 來裝載發(fā)送出去的消息和接收到的消息。然后底部寫了一個 TextField 控件來處理用戶的輸入和一個 button 來觸發(fā)發(fā)送的動作。
邏輯代碼
我們初始化對應控件并且 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中的顯示
}
①初始默認假數(shù)據(jù)
我們方便展示就寫了 3 條假數(shù)據(jù)僅供展示:
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 來做一個開關處理用戶輸入的,動作輪流來處理是接收到消息還是發(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;//消息的內容
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 布局預覽效果:
適配器邏輯代碼:
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 方法中通過小標 i 來遍歷集合然后拿到里面每一個對應里面的 type 屬性來判斷是顯示左邊布局還是右邊布局。
也就是對應的發(fā)送消息和接收消息的 UI,我們通過簡單布局顯示影藏來實現(xiàn)消息的左右兩邊顯示效果,到此整個仿微信聊天的布局 UI 效果就講完了 。
總結
鴻蒙的仿微信聊天 UI 效果實現(xiàn)起來相對比較簡單,其實還有一種辦法那就是 ListContainer 的多布局也是通過 bean 里面的標識來顯示左右不同的布局實現(xiàn)聊天界面的效果。因為篇幅有限這里就不展開講了有興趣的同學可以私下研究。最后希望我的文章能幫助到各位解決問題,以后我還會貢獻更多有用的代碼分享給大家。
項目地址:
https://gitee.com/qiuyu123/hms_im_demo
編輯:jq
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權轉載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規(guī)問題,請聯(lián)系本站處理。
舉報投訴
-
JAVA
+關注
關注
20文章
3001瀏覽量
116419 -
JS
+關注
關注
0文章
79瀏覽量
18988 -
ui
+關注
關注
0文章
209瀏覽量
22383 -
鴻蒙
+關注
關注
60文章
2963瀏覽量
45883
原文標題:鴻蒙版微信聊天UI效果實現(xiàn)!
文章出處:【微信號:gh_834c4b3d87fe,微信公眾號:OpenHarmony技術社區(qū)】歡迎添加關注!文章轉載請注明出處。
發(fā)布評論請先 登錄
相關推薦
熱點推薦
本地部署微信公眾號文章搜索MCP服務并實現(xiàn)遠程訪問
本文介紹如何在本地部署基于FastAPI的微信公眾號文章搜索MCP服務,并通過內網(wǎng)穿透實現(xiàn)公網(wǎng)訪問,支持關鍵詞搜索。
電能質量在線監(jiān)測裝置故障會發(fā)微信告警嗎?
電能質量在線監(jiān)測裝置本身通常不直接發(fā)送微信告警,但通過配套的云平臺 / 運維系統(tǒng)并結合網(wǎng)絡通信,可實現(xiàn)微信告警功能 ,將故障信息實時推送給相
分享---儲能UI界面能量流動動畫實現(xiàn)方法
本文分享 工商業(yè)儲能設備的UI界面中如何實現(xiàn) 能量流動的動畫效果。
本例子效果 基于拓普微工業(yè)級 7寸屏電容串口屏(HMT070ETA-D型
發(fā)表于 09-02 18:22
分享---簡單快速實現(xiàn)烘烤設備UI界面的方法
本文分享下,如何簡單快速的設計出工業(yè)烘烤設備的UI界面方法,
借助 \"墨刀\" 界面原型設計工具,設計烘烤機主界面圖片。
使用拓普微 SGTools開發(fā)工具,建立工程和頁面
發(fā)表于 08-26 11:58
微信小程序API集成京東庫存,移動端銷量暴漲!
。本文將探討如何通過微信小程序API集成京東庫存系統(tǒng),實現(xiàn)實時數(shù)據(jù)同步,并最終推動移動端銷量實現(xiàn)顯著增長。我們將一步步分析集成過程、核心優(yōu)勢,以及實際案例中的
鴻蒙應用px,vp,fp概念詳解
傳統(tǒng)移動端開發(fā)方向,轉到鴻蒙應用開發(fā)方向。 前端開發(fā)同學對于開發(fā)范式很熟悉,但是對于工作流程和開發(fā)方式是會有不適感,其實移動應用開發(fā)與前端開發(fā),最大的區(qū)別就在于UI適配和性能優(yōu)化上了。 今天我們就來分析下鴻蒙中
鴻蒙ArkTS+ArkUI仿微信消息列表頁制作
\'
})
這里使用了justifyContent屬性來進行居中,如果不加這個屬性的話,那么100%寬度的row會讓文字靠左顯示。接下來是顯示聊天數(shù)據(jù)的列表,這里采用ForEach列表渲染來實現(xiàn)。組件上的話
發(fā)表于 06-30 18:28
UI開發(fā)概述
的渲染效果。開發(fā)者可以將系統(tǒng)內置組件組合為自定義組件,通過這種方式將頁面組件化為一個個獨立的UI單元,實現(xiàn)頁面不同單元的獨立創(chuàng)建、開發(fā)和復用,具有更強的工程性。
頁面路由和組件導航
應用可能包含多個頁面
發(fā)表于 06-24 06:36
鴻蒙Next實現(xiàn)瀑布流布局
為了實現(xiàn)類似真實瀑布流不斷加載新數(shù)據(jù)的效果,可以結合鴻蒙的 LazyForEach 組件,在滾動到列表底部時觸發(fā)數(shù)據(jù)加載邏輯
六、網(wǎng)絡權限
// config.json
{
\"module
發(fā)表于 06-10 14:17
HarmonyOS實戰(zhàn):快遞信息時間軸效果實現(xiàn)
前言 快遞信息時間軸在購物軟件中是必不可少的功能,通過時間軸可以展示快遞從發(fā)貨到派送的每一個環(huán)節(jié)。本篇文章通過代碼的形式詳細講解在鴻蒙日常開發(fā)中如何實現(xiàn)時間軸的效果。(篇尾附有完整源碼) 實現(xiàn)
Kuikly鴻蒙版正式開源 —— 揭秘卓越性能適配之旅
的 ArkUI 來編寫的,UI組件由數(shù)據(jù)和UI描述組成,UI更新只能通過修改其綁定的數(shù)據(jù)來實現(xiàn)。渲染層怎樣驅動聲明式的ArkUI成為了鴻蒙版
發(fā)表于 06-04 16:46
鴻蒙版微信聊天UI效果實現(xiàn)!
評論