您好,登錄后才能下訂單哦!
這篇文章主要介紹了Android如何實(shí)現(xiàn)聊天界面,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體內(nèi)容如下
文件目錄
在app下的build.gradle中添加依賴庫(kù)(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依賴庫(kù) 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ù)定位到最后一行,最后清空輸入欄。
效果圖:
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Android如何實(shí)現(xiàn)聊天界面”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。