android recyclerview模拟聊天界面

所属分类: 软件编程 / Android 阅读数: 22
收藏 0 赞 0 分享

本文实例为大家分享了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 HapticFeedback(震动反馈)

下面小编就为大家带来一篇老生常谈Android HapticFeedback(震动反馈)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详谈OnTouchListener与OnGestureListener的区别

下面小编就为大家带来一篇详谈OnTouchListener与OnGestureListener的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿知乎悬浮功能按钮FloatingActionButton效果

前段时间在看属性动画,恰巧这个按钮的效果可以用属性动画实现,下面通过本文给大家分享adroid仿知乎悬浮功能按钮FloatingActionButton效果,需要的朋友参考下吧
收藏 0 赞 0 分享

解决Android V7后自定义Toolbar、ActionBar左侧有空白问题

这篇文章主要介绍的Android V7后自定义Toolbar、ActionBar左侧有空白问题的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android常见控件使用详解

这篇文章主要为大家详细介绍了Android常见控件的使用方法,包括ProgressBar进度条控件、AlertDialog对话框控件等,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android实现简洁的APP更新dialog数字进度条

这篇文章主要为大家详细介绍了Android实现简洁的APP更新dialog数字进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 判断当前语言环境是否是中文环境

本文主要介绍了Android 判断当前语言环境是否是中文环境的方法。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

详谈Android中Matrix的set、pre、post的区别

下面小编就为大家带来一篇详谈Android中Matrix的set、pre、post的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现登录界面记住密码的存储

这篇文章主要为大家详细介绍了Android SharedPreferrences实现登录界面记住密码的存储,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 使用SharedPreferrences储存密码登录界面记住密码功能

Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences, 其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入,下面通过实例代码给大家讲解下,需要的朋友参考下吧
收藏 0 赞 0 分享
查看更多