android recyclerview模拟聊天界面
作者:zhu6201976 时间:2022-01-19 17:02:42
本文实例为大家分享了android recyclerview模拟聊天界面的具体代码,供大家参考,具体内容如下
效果图:
实现代码:
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实现
* 效果不好,发送的消息不能靠右对齐,
* 不知何故,怎么弄都弄不好,请教!
* 问题的解决:用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);
}
/**
* 初始化数据源
*/
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();
// 如果用户没有输入,则是一个空串""
if (!content.isEmpty()) {
mMsgList.add(new Msg(content, Msg.TYPE_SEND));
// 通知数据适配器刷新界面
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="请输入要发送的内容" />
<Button
android:id="@+id/bt_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送" />
</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>
标签:android,recyclerview,聊天界面
0
投稿
猜你喜欢
java微信公众号支付开发之现金红包
2023-09-01 17:28:38
面试中遇到的java逃逸分析问题
2021-12-08 11:58:28
C++实现简易反弹小球游戏的示例代码
2023-06-15 08:18:50
JDK线程池和Spring线程池的使用实例解析
2023-02-24 13:27:32
一文详解Java抽象类到底有多抽象
2023-08-27 01:41:26
Android广播实现App开机自启动
2023-01-06 14:54:44
ReentrantLock从源码解析Java多线程同步学习
2023-10-13 02:32:55
android LinearLayout和RelativeLayout组合实现精确布局方法介绍
2021-06-17 12:33:33
深入浅出MyBatis中映射文件和实体类的关联性
2022-08-29 07:34:08
基于ClasspathResource路径问题的解决
2022-03-29 21:23:22
c#多线程之间的排他锁的实现
2022-06-11 12:31:53
Java构建高效结果缓存方法示例
2021-11-12 03:52:06
Java多线程之Park和Unpark原理
2023-03-29 15:46:11
C#使用FtpWebRequest与FtpWebResponse完成FTP操作
2021-08-24 04:58:26
详解Mybatis-plus中更新date类型数据遇到的坑
2022-08-17 21:43:14
简单实用的Android UI微博动态点赞效果
2023-01-07 06:32:54
详解Java中如何正确书写单例模式
2022-06-20 01:35:00
javaWeb项目部署到阿里云服务器步骤详解
2023-11-07 05:21:36
Java发送http请求的示例(get与post方法请求)
2021-09-29 23:21:40
C# 填充Excel图表、图例背景色的实例代码
2023-08-06 04:01:31