使用RecyclerView完成聊天界面以及消息的更新
2017-02-24 15:19
357 查看
github地址:https://github.com/skysunlei/Chat
代码上都有详细介绍,对RecyclerView不熟悉的朋友可以看一下我前几篇文章。别忘记对配置文件中对RecyclerView进行配置
MainActivity.XML文件
msgitem.xml
mainactivity.class
MsgAdapter.class
Msg.class
效果图
代码上都有详细介绍,对RecyclerView不熟悉的朋友可以看一下我前几篇文章。别忘记对配置文件中对RecyclerView进行配置
MainActivity.XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:background="#d8e0e8" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.sky.chat.MainActivity"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/recycler_view" ></android.support.v7.widget.RecyclerView> <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/send" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
msgitem.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="150dp" android:id="@+id/left_layout" android:layout_gravity="left" android:background="@drawable/message" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/left_msg" android:layout_gravity="center" android:layout_margin="10dp" android:textColor="#fff"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="150dp" android:id="@+id/right_layout" android:layout_gravity="right" android:background="@drawable/message" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/right_msg" android:layout_gravity="center" android:layout_margin="10dp"/> </LinearLayout> </LinearLayout>
mainactivity.class
package com.example.sky.chat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; 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 android.widget.LinearLayout; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<Msg> msgList = new ArrayList<>(); private EditText inputText; private Button send; private RecyclerView recyclerView; private MsgAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initMsgs();//初始化消息数据 inputText = (EditText) findViewById(R.id.input_text); send = (Button) findViewById(R.id.send); recyclerView = (RecyclerView) findViewById(R.id.recycler_view); LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); adapter = new MsgAdapter(msgList); recyclerView.setA bc4b dapter(adapter); send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String content = inputText.getText().toString(); if (!"".equals(content)){ Msg msg = new Msg(content,Msg.TYPE_SENT); msgList.add(msg); adapter.notifyItemInserted(msgList.size()-1);//当有新消息时,刷新RecyclerView中的显示 recyclerView.scrollToPosition(msgList.size() - 1);//将RecyclerView定位到最后一行 inputText.setText("");//清空输入框中的内容 } } }); } private void initMsgs() { Msg msg1 = new Msg("Hello guy",Msg.TYPE_RECEIVED); Msg msg2 = new Msg("Hello.Who is that?",Msg.TYPE_SENT); Msg msg3 = new Msg("This is Tom.Nice talking to you.",Msg.TYPE_RECEIVED); msgList.add(msg1); msgList.add(msg2); msgList.add(msg3); } }
MsgAdapter.class
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> { private List<Msg> mMsgList; static class ViewHolder extends RecyclerView.ViewHolder{ LinearLayout leftLayout; LinearLayout rightLayout; TextView leftMsg; TextView rightMsg; public ViewHolder(View itemView) { super(itemView); leftLayout = (LinearLayout) itemView.findViewById(R.id.left_layout); rightLayout = (LinearLayout) itemView.findViewById(R.id.right_layout); leftMsg = (TextView) itemView.findViewById(R.id.left_msg); rightMsg = (TextView) itemView.findViewById(R.id.right_msg); } } public MsgAdapter(List<Msg> mMsgList) { this.mMsgList = mMsgList; } @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 = mMsgList.get(position); if (msg.getType() == Msg.TYPE_RECEIVED){ //如果是收到的消息,则显示左边的消息布局,将右边的消息布局隐藏 holder.leftLayout.setVisibility(View.VISIBLE); holder.rightLayout.setVisibility(View.GONE); holder.leftMsg.setText(msg.getContent()); }else if (msg.getType() == Msg.TYPE_SENT){ //如果是发出的消息,则显示右边的消息布局,将左边的消息布局隐藏 holder.rightLayout.setVisibility(View.VISIBLE); holder.leftLayout.setVisibility(View.GONE); holder.rightMsg.setText(msg.getContent()); } } @Override public int getItemCount() { return mMsgList.size(); } }
Msg.class
public class Msg { public static final int TYPE_RECEIVED = 0; public static final int TYPE_SENT = 1; private String content; private int type; /** * content表示消息的内容,type表示消息的类型。其中消息类型有两个值可选,TYPE_RECEIVED表示这是一条收到的消息,TYPE_SENT表示这是一条发出的消息 * @param content * @param type */ public Msg(String content, int type) { this.content = content; this.type = type; } public int getType() { return type; } public String getContent() { return content; } }
效果图
相关文章推荐
- 使用RecyclerView编写聊天界面及demo
- 使用RecyclerView实现聊天界面
- 安卓中引入第三方架包butterKnife的操作方法以及使用Intent创建选择窗口和通过handler发消息实时更新textview中的时间
- RecyclerView直播聊天界面判断最后一条消息位置
- 聊天界面 使用listview和recyclerview
- 使用RecyclerView来完成聊天对话框
- MFC Dialog工程中多线程使用PostMessage发送接收字符串消息并更新界面
- iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- 纯代码实现QQ聊天界面---TableView使用详解
- iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建
- RecyclerView实现聊天界面功不能滑动到指定位置问题
- iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android-使用ViewGroup实现左右滑动切换界面以及界面裂开的效果
- SurfaceView(包含对消息传递的回顾以及对继承View类使用的回顾)制作指南针
- iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android httpGet 使用 以及使用Handler异步更新textview的text值