使用RecyclerView来完成聊天对话框
来源:互联网 发布:游族网络手机官网 编辑:程序博客网 时间:2024/06/10 20:59
聊天记录信息类,该类中2个成员变量聊天内容content和信息来源type
public class Msg{ public static final int TYPE_RECEIVE = 0; public static final int TYPE_SEND = 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; }}定义一个适配器用于RecyclerView数据适配,根据信息来源不同,选择性显示聊天记录信息
/** * Created by Xiongxl on 2016/12/20. */public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder>{ List<Msg> msgList; @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { Msg msg = msgList.get(position); if (msg.getType() == Msg.TYPE_RECEIVE) { holder.leftLayout.setVisibility(View.VISIBLE); holder.rightLayout.setVisibility(View.GONE); holder.leftMsg.setText(msg.getContent()); } else if (msg.getType() == Msg.TYPE_SEND) { holder.rightLayout.setVisibility(View.VISIBLE); holder.leftLayout.setVisibility(View.GONE); holder.rightMsg.setText(msg.getContent()); } } public MsgAdapter(List<Msg> msgs) { this.msgList = msgs; } @Override public int getItemCount() { return msgList.size(); } static class ViewHolder extends RecyclerView.ViewHolder { LinearLayout leftLayout; LinearLayout rightLayout; TextView leftMsg; TextView rightMsg; 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); } }}
recyclerView项目内容的布局文件,包含一个左边的聊天记录和右边的聊天记录
<?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="wrap_content" android:orientation="vertical"> <LinearLayout android:id="@+id/left_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:background="@drawable/left"> <TextView android:id="@+id/left_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp"/> </LinearLayout> <LinearLayout android:id="@+id/right_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:background="@drawable/right"> <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>项目的布局文件,布局文件中有一个recyclerView、EditText和一个Button用于信息发送触发事件
<?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:orientation="vertical" android:background="#d8e0e8"> <android.support.v7.widget.RecyclerView android:id="@+id/msg_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"> <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/btnSend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send"/> </LinearLayout></LinearLayout>MainActivity活动内的代码代码如下
public class MainActivity extends AppCompatActivity{ List<Msg> msgList = new ArrayList<>(); EditText inputText; Button btnSend; RecyclerView recyclerView; private MsgAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initMsg(); btnSend = (Button) this.findViewById(R.id.btnSend); inputText = (EditText) this.findViewById(R.id.input_text); recyclerView = (RecyclerView) this.findViewById(R.id.msg_recyclerView); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(linearLayoutManager); adapter = new MsgAdapter(msgList); recyclerView.setAdapter(adapter); btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String content = inputText.getText().toString().trim(); if (!content.equals("")) { msgList.add(new Msg(content, Msg.TYPE_SEND)); adapter.notifyItemInserted(msgList.size() - 1); recyclerView.scrollToPosition(msgList.size() - 1); inputText.setText(""); } } }); } private void initMsg() { Msg msg1 = new Msg("hello sealong", Msg.TYPE_RECEIVE); msgList.add(msg1); Msg msg2 = new Msg("hello peipei", Msg.TYPE_SEND); msgList.add(msg2); Msg msg = new Msg("What are you doing", Msg.TYPE_RECEIVE); msgList.add(msg); }}
数据集更改后,通知适配器有更改,并且将RecyclerView的滚动条滑到最后一个元素
adapter.notifyItemInserted(msgList.size() - 1); recyclerView.scrollToPosition(msgList.size() - 1);
1 1
- 使用RecyclerView来完成聊天对话框
- 使用RecyclerView完成聊天界面以及消息的更新
- 使用RecyclerView实现简单的聊天页面
- 聊天界面 使用listview和recyclerview
- 使用RecyclerView编写聊天界面及demo
- 使用sticky-headers-recyclerview 完成 recyclerview粘性头部
- 聊天对话框
- 使用MVP来实现recyclerview数据
- RecyclerView实现聊天界面
- 使用LayoutInflater做简单的android聊天对话框
- ADSI,使用ASP来完成NT管理
- ADSI,使用ASP来完成NT管理
- 使用DownloadProvider来完成下载任务
- 使用Kettle来完成数据迁移
- 使用DownloadProvider来完成下载任务
- 使用DownloadProvider来完成下载任务
- 使用DownloadProvider来完成下载任务
- 使用Struts来完成文件下载
- springboot使用
- 根据.def文件生成lib文件
- OneNote 目录.onetoc2 删除方法(日志)
- 彻底看懂Oracle License许可机制
- 大学生如何让自己强大起来(计算机、电子方向)
- 使用RecyclerView来完成聊天对话框
- Android中使用到的设计者模式
- JavaScriptCore学习总结
- 安卓常用1
- 同步 异步 AJAX JS
- 批量随机生成身份证号码
- Oracle的License
- 安卓上传图片
- php取随机数 explode劫取字符串 时间定义随机数