Android實現(xiàn)聊天界面
本文實例為大家分享了Android實現(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ā)送消息
定義消息的實體類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;
}
}- 用兩個常量來表示消息的類型(接收的還是發(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>- 將接收的消息居左對齊,發(fā)送的消息居右對齊
創(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();
}
}- 定義了一個內(nèi)部類ViewHolder,繼承自RecyclerView.ViewHolder。ViewHolder的構(gòu)造函數(shù)中傳入一個View參數(shù),這個參數(shù)通常是RecyclerView子項的最外層布局,這樣我們就可以通過findViewById()方法來獲取布局中的接收和發(fā)送消息布局的實例了。
- MsgAdapter中也有一個構(gòu)造函數(shù),將要展示的數(shù)據(jù)源傳進(jìn)來復(fù)制給mMsgList。
- MsgAdapter繼承自RecyclerView.Adapter,必須重寫onCreateViewHolder()、onBindViewHolder()、getItemCount()三個方法。
- onCreateViewHolder()用于創(chuàng)建ViewHolder實例,在這個方法中將msg_item布局加載進(jìn)來,然后創(chuàng)建一個ViewHolder實例,并把加載出來的布局傳到構(gòu)造函數(shù)中,返回實例。
- onBindViewHolder()用于對RecyclerView子項的數(shù)據(jù)進(jìn)行賦值。
- getItemCount()獲得RecyclerView有多少個子項
使用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的實例,然后創(chuàng)建了LinearLayoutManager對象,并把它設(shè)置到RecyclerView的實例中去。LayoutManager用于指定RecyclerView的布局方式,這里使用是線性布局的意思,可以實現(xiàn)ListView相同的效果。
設(shè)置了send按鈕的響應(yīng)事件,如果內(nèi)容不為空則創(chuàng)建出一個新的Msg對象,并添加到msgList中去,之后調(diào)用了適配器的方法notifyItemInserted()來通知列表有新數(shù)據(jù)插入,這樣新增的消息才能在RecyclerView中顯示。接著調(diào)用RecyclerView的scrollToPosition()方法,將顯示的數(shù)據(jù)定位到最后一行,最后清空輸入欄。
效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實現(xiàn)透明度可變的標(biāo)題欄效果
這篇文章主要介紹了Android實現(xiàn)透明度可變的標(biāo)題欄效果的相關(guān)資料,具有一定的參考價值,需要的朋友可以參考下2016-02-02
Android studio 連接手機調(diào)試操作步驟
這篇文章主要介紹了Android studio 連接手機調(diào)試操作步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Android貝塞爾曲線初步學(xué)習(xí)第三課 Android實現(xiàn)添加至購物車的運動軌跡
這篇文章主要為大家詳細(xì)介紹了Android貝塞爾曲線初步學(xué)習(xí)第三課,Android實現(xiàn)添加至購物車的運動軌跡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
Android scrollview實現(xiàn)底部繼續(xù)拖動查看圖文詳情
這篇文章主要為大家詳細(xì)介紹了Android scrollview實現(xiàn)底部繼續(xù)拖動查看圖文詳情,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02
Android使用Handler實現(xiàn)下載文件功能
這篇文章主要為大家詳細(xì)介紹了Android使用Handler實現(xiàn)下載文件功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06

