Android實(shí)現(xiàn)聊天界面
本文實(shí)例為大家分享了Android實(shí)現(xiàn)聊天界面的具體代碼,供大家參考,具體內(nèi)容如下
文件目錄

在app下的build.gradle中添加依賴庫(RecyclerView)
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.example.uibestpractice"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguarrules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:recyclerview-v7:24.2.1'//添加RecyclerView依賴庫
testCompile 'junit:junit:4.12'
}編寫主界面(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d8e0d8">
<android.support.v7.widget.RecyclerView
android:id="@+id/msg_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type something here"
android:maxLines="2"/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"/>
</LinearLayout>
</LinearLayout> - 在主界面中放置的RecyclerView用于顯示消息
- EditText用于編輯消息
- Button用于發(fā)送消息
定義消息的實(shí)體類Msg
package com.example.uibestpractice;
public class Msg {
public static final int TYPE_RECEIVED = 0;
public static final int TYPE_SENT = 1;
private String content;
private int type;
public Msg(String content,int type) {
this.content = content;
this.type = type;
}
public String getContent() {
return content;
}
public int getType() {
return type;
}
}- 用兩個(gè)常量來表示消息的類型(接收的還是發(fā)送的)
編寫RecyclerView的子布局(msg_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:id="@+id/left_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="@drawable/message_left">
<TextView
android:id="@+id/left_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#fff"/>
</LinearLayout>
<LinearLayout
android:id="@+id/right_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/message_right">
<TextView
android:id="@+id/right_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"/>
</LinearLayout>
</LinearLayout>- 將接收的消息居左對(duì)齊,發(fā)送的消息居右對(duì)齊
創(chuàng)建RecyclerView適配器類
package com.example.uibestpractice;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder>{
private List<Msg> mMsgList;
static class ViewHolder extends RecyclerView.ViewHolder {
LinearLayout leftLayout;
LinearLayout rightLayout;
TextView leftMsg;
TextView rihgtMsg;
public ViewHolder(View view) {
super(view);
leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);
rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
leftMsg = (TextView) view.findViewById(R.id.left_msg);
rihgtMsg = (TextView) view.findViewById(R.id.right_msg);
}
}
public MsgAdapter(List<Msg> msgList) {
mMsgList = msgList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Msg msg = mMsgList.get(position);
if (msg.getType() == Msg.TYPE_RECEIVED) {
holder.leftLayout.setVisibility(View.VISIBLE);
holder.rightLayout.setVisibility(View.GONE);
holder.leftMsg.setText(msg.getContent());
} else if (msg.getType() == Msg.TYPE_SENT) {
holder.rightLayout.setVisibility(View.VISIBLE);
holder.leftLayout.setVisibility(View.GONE);
holder.rihgtMsg.setText(msg.getContent());
}
}
@Override
public int getItemCount() {
return mMsgList.size();
}
}- 定義了一個(gè)內(nèi)部類ViewHolder,繼承自RecyclerView.ViewHolder。ViewHolder的構(gòu)造函數(shù)中傳入一個(gè)View參數(shù),這個(gè)參數(shù)通常是RecyclerView子項(xiàng)的最外層布局,這樣我們就可以通過findViewById()方法來獲取布局中的接收和發(fā)送消息布局的實(shí)例了。
- MsgAdapter中也有一個(gè)構(gòu)造函數(shù),將要展示的數(shù)據(jù)源傳進(jìn)來復(fù)制給mMsgList。
- MsgAdapter繼承自RecyclerView.Adapter,必須重寫onCreateViewHolder()、onBindViewHolder()、getItemCount()三個(gè)方法。
- onCreateViewHolder()用于創(chuàng)建ViewHolder實(shí)例,在這個(gè)方法中將msg_item布局加載進(jìn)來,然后創(chuàng)建一個(gè)ViewHolder實(shí)例,并把加載出來的布局傳到構(gòu)造函數(shù)中,返回實(shí)例。
- onBindViewHolder()用于對(duì)RecyclerView子項(xiàng)的數(shù)據(jù)進(jìn)行賦值。
- getItemCount()獲得RecyclerView有多少個(gè)子項(xiàng)
使用RecyclerView(修改MainActivity)
package com.example.uibestpractice;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Msg> msgList = new ArrayList<>();
private EditText inputText;
private Button send;
private RecyclerView msgRecyclerView;
private MsgAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initMsgs();
inputText = (EditText) findViewById(R.id.input_text);
send = (Button) findViewById(R.id.send);
msgRecyclerView = (RecyclerView) findViewById(R.id.msg_recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
msgRecyclerView.setLayoutManager(layoutManager);
adapter = new MsgAdapter(msgList);
msgRecyclerView.setAdapter(adapter);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = inputText.getText().toString();
if (!"".equals(content)) {
Msg msg = new Msg(content,Msg.TYPE_SENT);
msgList.add(msg);
adapter.notifyItemInserted(msgList.size()-1);
msgRecyclerView.scrollToPosition(msgList.size()-1);
inputText.setText("");
}
}
});
}
private void initMsgs() {
Msg msg1 = new Msg("Hello",Msg.TYPE_RECEIVED);
msgList.add(msg1);
Msg msg2 = new Msg("I'm John",Msg.TYPE_RECEIVED);
msgList.add(msg2);
Msg msg3 = new Msg("Hello",Msg.TYPE_SENT);
msgList.add(msg3);
}
}onCreate()方法中先獲得了RecyclerView的實(shí)例,然后創(chuàng)建了LinearLayoutManager對(duì)象,并把它設(shè)置到RecyclerView的實(shí)例中去。LayoutManager用于指定RecyclerView的布局方式,這里使用是線性布局的意思,可以實(shí)現(xiàn)ListView相同的效果。
設(shè)置了send按鈕的響應(yīng)事件,如果內(nèi)容不為空則創(chuàng)建出一個(gè)新的Msg對(duì)象,并添加到msgList中去,之后調(diào)用了適配器的方法notifyItemInserted()來通知列表有新數(shù)據(jù)插入,這樣新增的消息才能在RecyclerView中顯示。接著調(diào)用RecyclerView的scrollToPosition()方法,將顯示的數(shù)據(jù)定位到最后一行,最后清空輸入欄。
效果圖:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android基于MLKit實(shí)現(xiàn)條形碼掃碼的代碼示例
這篇文章將借助開源庫?MLKit?實(shí)現(xiàn)條形碼掃描,對(duì)于商品條形碼也可以很好地識(shí)別成功,該庫的使用內(nèi)容非常豐富,除了條碼識(shí)別,還有文字識(shí)別、圖像標(biāo)記、人臉檢測等等,本文篇文章就只介紹最基本的條形碼掃描使用,需要的朋友可以參考下2023-08-08
RecyclerView 源碼淺析測量 布局 繪制 預(yù)布局
這篇文章主要介紹了RecyclerView 源碼淺析測量 布局 繪制 預(yù)布局,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
一文詳解如何在Flutter中使用導(dǎo)航Navigator
一個(gè)APP如果沒有頁面跳轉(zhuǎn)那么是沒有靈魂的,頁面跳轉(zhuǎn)的一個(gè)常用說法就是Navigator。那么在flutter中如何使用Navigator呢?本文就來和大家詳細(xì)聊聊2023-02-02
Android優(yōu)化查詢加載大數(shù)量的本地相冊圖片
本文介紹了Android優(yōu)化查詢加載大數(shù)量的本地相冊圖片,可以方便的照片的查詢,,感興趣的小伙伴們可以參考一下。2016-10-10
Android view更改背景資源與padding消失的問題解決辦法
這篇文章主要介紹了Android view更改背景資源與padding消失的問題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android開源AndroidSideMenu實(shí)現(xiàn)抽屜和側(cè)滑菜單
這篇文章主要為大家詳細(xì)介紹了Android開源AndroidSideMenu實(shí)現(xiàn)抽屜和側(cè)滑菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android 單例模式的四種實(shí)現(xiàn)方式
單例模式作為設(shè)計(jì)模式之一,使用場景非常多。本文講述了Android實(shí)現(xiàn)單例模式的幾種方式2021-05-05
android實(shí)現(xiàn)文字水印效果 支持多行水印
這篇文章主要為大家詳細(xì)介紹了android添加文字水印,并支持多行水印,自定義角度和文字大小,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10

