Android利用RecyclerView編寫(xiě)聊天界面
本文實(shí)例為大家分享了Android RecyclerView編寫(xiě)聊天界面的具體代碼,供大家參考,具體內(nèi)容如下
1、待會(huì)兒會(huì)用到RecyclerView,首先在app/build.gradle(注意有兩個(gè)build.gradle,選擇app下的那個(gè))當(dāng)中添加依賴(lài)庫(kù),如下:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:recyclerview-v7:24.2.1'
testCompile 'junit:junit:4.12'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
}
添加完之后記得點(diǎn)擊Sync Now進(jìn)行同步。
2、開(kāi)始編寫(xiě)主界面,修改activity_main.xml中的代碼,如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#d8e0e8" > <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用于顯示聊天的消息內(nèi)容(因?yàn)椴皇莾?nèi)置在系統(tǒng)SDK中的,所以需要把完整的包路徑寫(xiě)出來(lái));
放置一個(gè)EditView用于輸入消息,一個(gè)Button用于發(fā)送消息。
3、定義消息的實(shí)體類(lèi),新建Msg,代碼如下:
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;
}
}
Msg只有兩個(gè)字段,content表示消息的內(nèi)容,type表示消息的類(lèi)型(二值可選,一個(gè)是TYPE_RECRIVED,一個(gè)是TYPE_SENT)。
4、接著編寫(xiě)RecyclerView子項(xiàng)的布局,新建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="283dp" android:layout_height="106dp" android:layout_gravity="left" android:background="@drawable/zuo" android:weightSum="1"> <TextView android:id="@+id/left_msg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" /> </LinearLayout> <LinearLayout android:id="@+id/right_layout" android:layout_width="229dp" android:layout_height="109dp" android:layout_gravity="right" android:background="@drawable/you" > <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ì)齊,并用相應(yīng)的圖片作為背景。
5、創(chuàng)建RecyclerView的適配器類(lèi),新建MsgAdapter,代碼如下:
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 rightMsg;
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);
rightMsg=(TextView)view.findViewById(R.id.right_msg);
}
}
public MsgAdapter(List<Msg> msgList){
mMsgList=msgList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
//onCreateViewHolder()用于創(chuàng)建ViewHolder實(shí)例
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
return new ViewHolder(view);
//把加載出來(lái)的布局傳到構(gòu)造函數(shù)中,再返回
}
@Override
public void onBindViewHolder(ViewHolder Holder,int position){
//onBindViewHolder()用于對(duì)RecyclerView子項(xiàng)的數(shù)據(jù)進(jìn)行賦值,會(huì)在每個(gè)子項(xiàng)被滾動(dòng)到屏幕內(nèi)的時(shí)候執(zhí)行
Msg msg=mMsgList.get(position);
if(msg.getType()==Msg.TYPE_RECEIVED){
//增加對(duì)消息類(lèi)的判斷,如果這條消息是收到的,顯示左邊布局,是發(fā)出的,顯示右邊布局
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.rightMsg.setText(msg.getContent());
}
}
@Override
public int getItemCount(){
return mMsgList.size();
}
}
6、最后修改MainActivity中的代碼,來(lái)為RecyclerView初始化一些數(shù)據(jù),并給發(fā)送按鈕加入事件響應(yīng),代碼如下:
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(); //初始化消息數(shù)據(jù)
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);
//LinearLayoutLayout即線(xiàn)性布局,創(chuàng)建對(duì)象后把它設(shè)置到RecyclerView當(dāng)中
msgRecyclerView.setLayoutManager(layoutManager);
adapter=new MsgAdapter(msgList);
//創(chuàng)建MsgAdapter的實(shí)例并將數(shù)據(jù)傳入到MsgAdapter的構(gòu)造函數(shù)中
msgRecyclerView.setAdapter(adapter);
send.setOnClickListener(new View.OnClickListener(){
//發(fā)送按鈕點(diǎn)擊事件
@Override
public void onClick(View v){
String content=inputText.getText().toString();
//獲取EditText中的內(nèi)容
if(!"".equals(content)){
//內(nèi)容不為空則創(chuàng)建一個(gè)新的Msg對(duì)象,并把它添加到msgList列表中
Msg msg=new Msg(content,Msg.TYPE_SENT);
msgList.add(msg);
adapter.notifyItemInserted(msgList.size()-1);
//調(diào)用適配器的notifyItemInserted()用于通知列表有新的數(shù)據(jù)插入,這樣新增的一條消息才能在RecyclerView中顯示
msgRecyclerView.scrollToPosition(msgList.size()-1);
//調(diào)用scrollToPosition()方法將顯示的數(shù)據(jù)定位到最后一行,以保證可以看到最后發(fā)出的一條消息
inputText.setText("");
//調(diào)用EditText的setText()方法將輸入的內(nèi)容清空
}
}
});
}
private void initMsgs(){
Msg msg1=new Msg("Hello guy.",Msg.TYPE_RECEIVED);
msgList.add(msg1);
Msg msg2=new Msg("Hello.Who is that?",Msg.TYPE_SENT);
msgList.add(msg2);
Msg msg3=new Msg("This is Tom!",Msg.TYPE_RECEIVED);
msgList.add(msg3);
}
}
運(yùn)行程序,效果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程計(jì)算函數(shù)時(shí)間戳的相關(guān)方法總結(jié)
這篇文章主要介紹了Android編程計(jì)算函數(shù)時(shí)間戳的相關(guān)方法,結(jié)合實(shí)例形式總結(jié)分析了Android Java、Native、Kernel時(shí)間戳計(jì)算相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
Android ListView實(shí)現(xiàn)下拉頂部圖片變大效果
這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)下拉頂部圖片變大,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android自定義view實(shí)現(xiàn)水波紋進(jìn)度球效果
在我們的日常開(kāi)發(fā)中自定義控件還是用的挺多的,設(shè)計(jì)師或者產(chǎn)品為了更好的漂亮,美觀(guān),交互都會(huì)做一些牛逼的ui效果圖,但是最后實(shí)現(xiàn)的還是我們程序員啊。所以說(shuō) 自定義view你還是得會(huì)的。2016-08-08
探討:android項(xiàng)目開(kāi)發(fā) 統(tǒng)籌兼顧 需要考慮的因素
本篇文章是對(duì)基于android項(xiàng)目開(kāi)發(fā) 統(tǒng)籌兼顧 需要考慮的因素進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Jenkins打包android應(yīng)用時(shí)自動(dòng)簽名apk詳解
這篇文章主要介紹了Jenkins打包android應(yīng)用時(shí)自動(dòng)簽名apk詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Android Studio error: Unable to start the daemon process的解決方
這篇文章主要介紹了在 Android Studio 上新建項(xiàng)目,出現(xiàn) Unable to start the daemon process問(wèn)題的幾種的解決方法,需要的朋友可以參考下2020-10-10
Android數(shù)據(jù)流之Channel和Flow實(shí)現(xiàn)原理和技巧詳解
在 Android 應(yīng)用程序的開(kāi)發(fā)中,處理異步數(shù)據(jù)流是一個(gè)常見(jiàn)的需求,為了更好地應(yīng)對(duì)這些需求,Kotlin 協(xié)程引入了 Channel 和 Flow,它們提供了強(qiáng)大的工具來(lái)處理數(shù)據(jù)流,本文將深入探討 Channel 和 Flow 的內(nèi)部實(shí)現(xiàn)原理、高級(jí)使用技巧以及如何在 Android 開(kāi)發(fā)中充分利用它們2023-11-11
android實(shí)現(xiàn)手機(jī)App實(shí)現(xiàn)拍照功能示例
本篇文章主要介紹了android實(shí)現(xiàn)手機(jī)App實(shí)現(xiàn)拍照功能示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
Android中ActionBar和ToolBar添加返回箭頭的實(shí)例代碼
這篇文章主要介紹了Android中ActionBar和ToolBar添加返回箭頭的實(shí)例代碼,需要的朋友可以參考下2017-09-09
Android 中ListView setOnItemClickListener點(diǎn)擊無(wú)效原因分析
這篇文章主要介紹了Android 中ListView setOnItemClickListener點(diǎn)擊無(wú)效原因分析的相關(guān)資料,需要的朋友可以參考下2016-01-01

