android recyclerview模擬聊天界面
本文實(shí)例為大家分享了android recyclerview模擬聊天界面的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:

實(shí)現(xiàn)代碼:
package com.itheima74.chatui;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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;
/**
* 聊天界面,使用recyclerview實(shí)現(xiàn)
* 效果不好,發(fā)送的消息不能靠右對(duì)齊,
* 不知何故,怎么弄都弄不好,請(qǐng)教!
* 問題的解決:用Relativelayout代替linearlayout可以解決上述問題
*/
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerview;
private EditText et_input;
private ArrayList<Msg> mMsgList;
private MsgAdapter mMsgAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initAdapter();
}
private void initAdapter() {
mMsgAdapter = new MsgAdapter(mMsgList);
recyclerview.setAdapter(mMsgAdapter);
}
/**
* 初始化數(shù)據(jù)源
*/
private void initData() {
mMsgList = new ArrayList<>();
mMsgList.add(new Msg("Hello!", Msg.TYPE_RECEIVE));
mMsgList.add(new Msg("Hello! Who is that?", Msg.TYPE_SEND));
mMsgList.add(new Msg("This is Jack,Nice to meet you!", Msg.TYPE_RECEIVE));
}
/**
* 初始化控件
*/
private void initView() {
recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
et_input = (EditText) findViewById(R.id.et_input);
Button bt_send = (Button) findViewById(R.id.bt_send);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerview.setLayoutManager(layoutManager);
bt_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = et_input.getText().toString().trim();
// 如果用戶沒有輸入,則是一個(gè)空串""
if (!content.isEmpty()) {
mMsgList.add(new Msg(content, Msg.TYPE_SEND));
// 通知數(shù)據(jù)適配器刷新界面
mMsgAdapter.notifyDataSetChanged();
// 定位到最后一行
recyclerview.scrollToPosition(mMsgList.size() - 1);
// 輸入框置空
et_input.setText("");
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#d8e0e8" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/et_input" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="請(qǐng)輸入要發(fā)送的內(nèi)容" /> <Button android:id="@+id/bt_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="發(fā)送" /> </LinearLayout> </LinearLayout>
package com.itheima74.chatui;
/**
* Created by My on 2017/3/3.
*/
class Msg {
static final int TYPE_RECEIVE = 1;
static final int TYPE_SEND = 2;
String content;
int type;
Msg(String content, int type) {
this.content = content;
this.type = type;
}
}
package com.itheima74.chatui;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by My on 2017/3/3.
*/
class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
private ArrayList<Msg> mMsgList;
MsgAdapter(ArrayList<Msg> mMsgList) {
this.mMsgList = mMsgList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(parent.getContext(), R.layout.recyclerview_item, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Msg msg = mMsgList.get(position);
if (msg.type == Msg.TYPE_RECEIVE) {
holder.tv_receive.setVisibility(View.VISIBLE);
holder.tv_send.setVisibility(View.GONE);
holder.tv_receive.setText(msg.content);
} else {
holder.tv_send.setVisibility(View.VISIBLE);
holder.tv_receive.setVisibility(View.GONE);
holder.tv_send.setText(msg.content);
}
}
@Override
public int getItemCount() {
return mMsgList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
private TextView tv_receive;
private TextView tv_send;
ViewHolder(View itemView) {
super(itemView);
tv_receive = (TextView) itemView.findViewById(R.id.tv_receive);
tv_send = (TextView) itemView.findViewById(R.id.tv_send);
}
}
}
xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> <TextView android:id="@+id/tv_receive" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/message_left" android:gravity="center" android:text="who?" android:textSize="20sp" /> <TextView android:id="@+id/tv_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@id/tv_receive" android:background="@drawable/message_right" android:gravity="center" android:text="i am your father" android:textSize="20sp" /> </RelativeLayout>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android studio 3.6.1導(dǎo)入項(xiàng)目報(bào)錯(cuò)提示無法下載classpath里的內(nèi)容
這篇文章主要介紹了android studio 3.6.1導(dǎo)入項(xiàng)目報(bào)錯(cuò)提示無法下載classpath里的內(nèi)容,本文通過原因分析通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Android橫豎屏切換及其對(duì)應(yīng)布局加載問題詳解
這篇文章主要為大家詳細(xì)介紹了Android橫豎屏切換及其對(duì)應(yīng)布局加載問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Android無障礙自動(dòng)化結(jié)合opencv實(shí)現(xiàn)支付寶能量自動(dòng)收集操作方法
opencv可以進(jìn)行圖像識(shí)別,兩者結(jié)合在一起即可實(shí)現(xiàn)支付寶能量自動(dòng)收集,opencv用于識(shí)別能量,無障礙服務(wù)用于模擬手勢,即點(diǎn)擊能量,這篇文章主要介紹了Android無障礙自動(dòng)化結(jié)合opencv實(shí)現(xiàn)支付寶能量自動(dòng)收集,需要的朋友可以參考下2024-07-07
Android LinearLayout實(shí)現(xiàn)自動(dòng)換行效果
這篇文章主要為大家詳細(xì)介紹了Android LinearLayout實(shí)現(xiàn)自動(dòng)換行效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android 打開相冊(cè)選擇單張圖片實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 打開相冊(cè)選擇單張圖片實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android中實(shí)現(xiàn)iOS中的毛玻璃效果
為了實(shí)現(xiàn)毛玻璃效果,我們需要一組compute kernels(.rs文件中編寫),及一組用于控制renderScript相關(guān)的Javaapi(.rs文件自動(dòng)生成為Java類)。 這篇文章主要介紹了Android中實(shí)現(xiàn)iOS中的毛玻璃效果,需要的朋友可以參考下2017-06-06
Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法
本篇文章小編為大家介紹,Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法。需要的朋友參考下2013-04-04
Android 使用RecycleView列表實(shí)現(xiàn)加載更多的示例代碼
這篇文章主要介紹了Android 使用RecycleView列表實(shí)現(xiàn)加載更多的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05

