Android利用RecyclerView编写聊天界面

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

本文实例为大家分享了Android RecyclerView编写聊天界面的具体代码,供大家参考,具体内容如下

1、待会儿会用到RecyclerView,首先在app/build.gradle(注意有两个build.gradle,选择app下的那个)当中添加依赖库,如下:

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 compile 'com.android.support:appcompat-v7:24.2.1'
 compile 'com.android.support:recyclerview-v7:24.2.1'
 testCompile 'junit:junit:4.12'
 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
  exclude group: 'com.android.support', module: 'support-annotations'
 })
}

添加完之后记得点击Sync Now进行同步。

2、开始编写主界面,修改activity_main.xml中的代码,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/activity_main"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#d8e0e8"
 >
 <android.support.v7.widget.RecyclerView
  android:id="@+id/msg_recycler_view"
  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/send"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="send"
   />
 </LinearLayout>
</LinearLayout>

RecyclerView用于显示聊天的消息内容(因为不是内置在系统SDK中的,所以需要把完整的包路径写出来);

放置一个EditView用于输入消息,一个Button用于发送消息。

3、定义消息的实体类,新建Msg,代码如下:

public class Msg {
 public static final int TYPE_RECEIVED=0;
 public static final int TYPE_SENT=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;
 }
}

Msg只有两个字段,content表示消息的内容,type表示消息的类型(二值可选,一个是TYPE_RECRIVED,一个是TYPE_SENT)。

4、接着编写RecyclerView子项的布局,新建msg_item.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:id="@+id/left_layout"
  android:layout_width="283dp"
  android:layout_height="106dp"
  android:layout_gravity="left"
  android:background="@drawable/zuo"
  android:weightSum="1">

  <TextView
   android:id="@+id/left_msg"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:layout_margin="10dp"
   />
 </LinearLayout>

 <LinearLayout
  android:id="@+id/right_layout"
  android:layout_width="229dp"
  android:layout_height="109dp"
  android:layout_gravity="right"
  android:background="@drawable/you"
  >
  <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>

收到的消息局左对齐,发出的消息居右对齐,并用相应的图片作为背景。

5、创建RecyclerView的适配器类,新建MsgAdapter,代码如下:

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 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);
  }
 }
 public MsgAdapter(List<Msg> msgList){
  mMsgList=msgList;
 }
 @Override
 public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){    
 //onCreateViewHolder()用于创建ViewHolder实例
  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){      
 //onBindViewHolder()用于对RecyclerView子项的数据进行赋值,会在每个子项被滚动到屏幕内的时候执行
  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();
 }
}

6、最后修改MainActivity中的代码,来为RecyclerView初始化一些数据,并给发送按钮加入事件响应,代码如下:

public class MainActivity extends AppCompatActivity {
 private List<Msg> msgList=new ArrayList<>();
 private EditText inputText;
 private Button send;
 private RecyclerView msgRecyclerView;
 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);
  msgRecyclerView=(RecyclerView)findViewById(R.id.msg_recycler_view);

  LinearLayoutManager layoutManager=new LinearLayoutManager(this); 

  //LinearLayoutLayout即线性布局,创建对象后把它设置到RecyclerView当中
  msgRecyclerView.setLayoutManager(layoutManager);

  adapter=new MsgAdapter(msgList);         

  //创建MsgAdapter的实例并将数据传入到MsgAdapter的构造函数中
  msgRecyclerView.setAdapter(adapter);

  send.setOnClickListener(new View.OnClickListener(){     

 //发送按钮点击事件
   @Override
   public void onClick(View v){
    String content=inputText.getText().toString();    

  //获取EditText中的内容
    if(!"".equals(content)){         

  //内容不为空则创建一个新的Msg对象,并把它添加到msgList列表中
     Msg msg=new Msg(content,Msg.TYPE_SENT);
     msgList.add(msg);
     adapter.notifyItemInserted(msgList.size()-1);   

  //调用适配器的notifyItemInserted()用于通知列表有新的数据插入,这样新增的一条消息才能在RecyclerView中显示
     msgRecyclerView.scrollToPosition(msgList.size()-1); 

  //调用scrollToPosition()方法将显示的数据定位到最后一行,以保证可以看到最后发出的一条消息
     inputText.setText("");         

  //调用EditText的setText()方法将输入的内容清空
    }
   }
  });
 }

 private void initMsgs(){
  Msg msg1=new Msg("Hello guy.",Msg.TYPE_RECEIVED);
  msgList.add(msg1);
  Msg msg2=new Msg("Hello.Who is that?",Msg.TYPE_SENT);
  msgList.add(msg2);
  Msg msg3=new Msg("This is Tom!",Msg.TYPE_RECEIVED);
  msgList.add(msg3);
 }
}

运行程序,效果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多