当前位置: 移动技术网 > IT编程>移动开发>Android > Android利用RecyclerView编写聊天界面

Android利用RecyclerView编写聊天界面

2019年07月24日  | 移动技术网IT编程  | 我要评论

ca1205,赤色石,danshen

本文实例为大家分享了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);
 }
}

运行程序,效果如下:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网