android recyclerview模拟聊天界面
作者:zhu6201976 发布时间:2022-01-19 17:02:42
标签:android,recyclerview,聊天界面
本文实例为大家分享了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>
0
投稿
猜你喜欢
- 1.实例1(主要看到[2])1.1.系统功能: 开发一个计算器服务CalculateService,这个服务包含加(plus)、减(minu
- 一、项目简述功能: 主页显示商品; 所有二手书商品展示,可进行商品搜索; 点击商品进入商品详情页,具有立即购买和加入购物车功能,可增减购买商
- 我们知道,(1)如果是整百的年份,能被400整除的,是闰年;(2)如果不是整百的年份,能被4整除的,也是闰年。每400年,有97个闰年。鉴于
- 本文实例为大家分享了Java使用开源Rxtx实现串口通讯的具体代码,供大家参考,具体内容如下使用方法:windows平台:1、把rxtxPa
- SpringMVC * 介绍springMVC 中的 * 用于拦截控制器方法的执行。先创建出前置需要的一些条件:<a th:href=
- 一、分布式压测原理如下图(这个图说明的是要一台控制机,然后由这台控制机发压测脚本到每台远程执行机,然后由控制机收集执行机结果)二、修改 Jm
- java中next与nextLine用法区别:next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab键或E
- 本文实例为大家分享了Java网络编程TCP程序设计的具体代码,供大家参考,具体内容如下[1] TCP编程的主要步骤客户端(client):1
- 1. Resource Basics(1) Manifest Resources(资源清单)资源在编译期间添加到程序集。如果要将资源嵌入到程
- 1 关于自动内存管理Java是由jvm来管理内存,包括自动分配以及自动回收,因此它不容易出现内存泄漏和内存溢出问题。C/C++,由程序员手动
- 一、首先编写一个工具类Hello:public class Hello { public static void say(Str
- 一、关于堆JDK1.8中的PriortyQueue(优先级队列)底层使用了堆的数据结构,而堆实际就是在完全二叉树的基础之上进行了一些元素的调
- 说到java中的重载和覆盖呢,大家都很熟悉了吧,但是呢我今天就要写这个。本文主题:一.什么是重载二.什么是覆盖三.两者之间的区别重载(ove
- Elasticsearch简介Elasticsearch是什么?它能干什么?Elasticsearch(以下称之为ES)是一款基于Lucen
- maven thin jar 步骤spring-boot-maven-plugin configuration &
- 一、背景今天心血来潮,准备测试一下项目中 logback 的自动刷新功能,但是测试时发现并不生效。logback 的配置如下:<con
- 单个和批量定义别名typeAliases使用Mybatis的别名typeAliases可以在xml文件里非常方便的使用类,而不需要写出这个类
- 本文实例为大家分享了java就业信息管理平台开发案例,供大家参考,具体内容如下可查询公司信息,学生信息,班级信息,针对学生就业与否信息的统计
- 堆区:只存放类对象,线程共享;方法区:又叫静态存储区,存放class文件和静态数据,线程共享;栈区:存放方法局部变量,基本类型变量区、执行环
- 标识接口是没有任何方法和属性的接口。标识接口不对实现它的类有任何语义上的要求,它仅仅表明实现它的类属于一个特定的类型。标接口在Java语言中