最近開發(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ù)覽效果:
適配器邏輯代碼:
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-
JAVA
+關(guān)注
關(guān)注
19文章
2943瀏覽量
104101 -
JS
+關(guān)注
關(guān)注
0文章
75瀏覽量
17983 -
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)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論