关于如何通过recycleview实现聊天界面的效果

首先定义主布局文件

 

<?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="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <include
        android:id="@+id/ic_chart"
        layout="@layout/public_toolbar" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/ic_chart"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView

            android:id="@+id/lv_chart"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <include layout="@layout/chat_footbar" />
    </LinearLayout>

</RelativeLayout>

 

效果图

 

定义一个适配器

 

package com.petsknow.doctor.sessionmodule.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.chat.EMMessage;
import com.easemob.chat.ImageMessageBody;
import com.easemob.chat.TextMessageBody;
import com.easemob.util.DateUtils;
import com.petsknow.doctor.R;
import com.petsknow.doctor.commonmodule.activity.PhotoBrowerActivity;
import com.petsknow.doctor.commonmodule.constant.Constant;
import com.petsknow.doctor.commonmodule.constant.ConstantUrl;
import com.petsknow.doctor.commonmodule.glide.GlideUtils;
import com.petsknow.doctor.commonmodule.utils.L;
import com.petsknow.doctor.sessionmodule.utils.SmileUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by yukuo on 2016/3/5.
 * 这是一个聊天界面的适配器
 */
public class ChartListVIewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<EMMessage> list;
    private Intent intent;
    private Activity activity;
    private String avator;

    public ChartListVIewAdapter(List<EMMessage> list, Activity activity, String avator) {
        super();
        this.list = list;
        this.activity = activity;
        this.avator = avator;
    }

    /**
     * 这是一个添加一条数据并刷新界面的方法
     *
     * @param msg
     */
    public void addData(EMMessage msg) {
        list.add(list.size(), msg);
        notifyItemInserted(list.size());
    }

    /**
     * 这是一个根据不同的条目返回不同的类型的布局的方法
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        EMMessage msg = list.get(position);
        if (msg.direct == EMMessage.Direct.SEND) {
            if (msg.getType() == EMMessage.Type.TXT) {
                return Constant.SENDTXT;
            } else if (msg.getType() == EMMessage.Type.IMAGE) {
                return Constant.SENDIMAGE;
            }
        } else if (msg.direct == EMMessage.Direct.RECEIVE) {
            if (msg.getType() == EMMessage.Type.TXT) {
                return Constant.FROMTXT;
            } else if (msg.getType() == EMMessage.Type.IMAGE) {
                return Constant.FROMIMAGE;
            }
        }

        return super.getItemViewType(position);
    }

    /**
     * 根据不同的类型返回不同的holder
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == Constant.SENDTXT) {
            //发送者文本
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_send_txt, parent, false);
            return new SendTxtVIewHolder(view);
        } else if (viewType == Constant.SENDIMAGE) {
            //发送者图片
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_send_image, parent, false);
            return new SendImageVIewHolder(view);
        } else if (viewType == Constant.FROMTXT) {
            //接受者文本
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_from_txt, parent, false);
            return new FromTxtVIewHolder(view);
        } else if (viewType == Constant.FROMIMAGE) {
            //接受者图片
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_from_image, parent, false);
            return new FromImageVIewHolder(view);
        }
        return null;
    }

    /**
     * 这是一个绑定数据的方法
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SendTxtVIewHolder) {
            TextMessageBody txtbody = (TextMessageBody) list.get(position).getBody();
            String content = txtbody.getMessage();

            ((SendTxtVIewHolder) holder).tv_item_send_txt.setText(SmileUtils.getSmiledText(activity, content));
            // 两条消息时间离得如果稍长,显示时间
            if (position == 0) {

            } else {
                if (DateUtils.isCloseEnough(list.get(position).getMsgTime(), list.get(position - 1).getMsgTime())) {
                    ((SendTxtVIewHolder) holder).tv_send_msg_date.setVisibility(View.GONE);
                } else {
                    ((SendTxtVIewHolder) holder).tv_send_msg_date.setText(DateUtils.getTimestampString(new Date(list.get(position).getMsgTime())));
                    ((SendTxtVIewHolder) holder).tv_send_msg_date.setVisibility(View.VISIBLE);
                }
            }

        } else if (holder instanceof SendImageVIewHolder) {
            final ImageMessageBody imageMessageBody = (ImageMessageBody) list.get(position).getBody();
            L.i("图片地址", imageMessageBody.getLocalUrl() + "****" + imageMessageBody.getRemoteUrl());
            GlideUtils.roundImageCenterGroup(imageMessageBody.getLocalUrl(), ((SendImageVIewHolder) holder).iv_item_send_image, 8);
            ((SendImageVIewHolder) holder).iv_item_send_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    intent = new Intent(activity, PhotoBrowerActivity.class);
                    intent.putExtra("url", imageMessageBody.getLocalUrl());
                    activity.startActivity(intent);
                }
            });
        } else if (holder instanceof FromTxtVIewHolder) {
            TextMessageBody txtbody = (TextMessageBody) list.get(position).getBody();
            String content = txtbody.getMessage();
            ((FromTxtVIewHolder) holder).tv_item_from_txt.setText(SmileUtils.getSmiledText(activity, content));
            GlideUtils.circleImage(ConstantUrl.qiniu + avator, ((FromTxtVIewHolder) holder).from_person_avator);
            // 两条消息时间离得如果稍长,显示时间
            if (position == 0) {

            } else {
                if (DateUtils.isCloseEnough(list.get(position).getMsgTime(), list.get(position - 1).getMsgTime())) {
                    ((FromTxtVIewHolder) holder).tv_from_msg_date.setVisibility(View.GONE);
                } else {
                    ((FromTxtVIewHolder) holder).tv_from_msg_date.setText(DateUtils.getTimestampString(new Date(list.get(position).getMsgTime())));
                    ((FromTxtVIewHolder) holder).tv_from_msg_date.setVisibility(View.VISIBLE);
                }
            }
        } else if (holder instanceof FromImageVIewHolder) {
            final ImageMessageBody imageMessageBody = (ImageMessageBody) list.get(position).getBody();
            L.i("图片地址接受者", imageMessageBody.getLocalUrl() + "****" + imageMessageBody.getRemoteUrl());
            GlideUtils.circleImage(ConstantUrl.qiniu + avator, ((FromImageVIewHolder) holder).iv_item_from_image);
            GlideUtils.roundImageCenterGroup(ConstantUrl.qiniu + imageMessageBody.getRemoteUrl(), ((FromImageVIewHolder) holder).from_person_avator, 8);
            ((FromImageVIewHolder) holder).iv_item_from_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    intent = new Intent(activity, PhotoBrowerActivity.class);
                    intent.putExtra("url", imageMessageBody.getRemoteUrl());
                    activity.startActivity(intent);
                }
            });
        }
    }

    /**
     * 条目个数
     * @return
     */
    @Override
    public int getItemCount() {
        return list.size();
    }

    /**
     * 以下四个内部类为定义的不同的holder
     */
    class SendTxtVIewHolder extends RecyclerView.ViewHolder {
        private TextView tv_item_send_txt;
        private TextView tv_send_msg_date;

        public SendTxtVIewHolder(View view) {
            super(view);
            tv_item_send_txt = (TextView) view.findViewById(R.id.tv_item_send_txt);
            tv_send_msg_date = (TextView) view.findViewById(R.id.tv_send_msg_date);
        }
    }

    class SendImageVIewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_item_send_image;

        public SendImageVIewHolder(View view) {
            super(view);
            iv_item_send_image = (ImageView) view.findViewById(R.id.iv_send_image);
        }
    }

    class FromTxtVIewHolder extends RecyclerView.ViewHolder {
        private TextView tv_item_from_txt;
        private ImageView from_person_avator;
        private TextView tv_from_msg_date;

        public FromTxtVIewHolder(View view) {
            super(view);
            tv_item_from_txt = (TextView) view.findViewById(R.id.tv_item_from_txt);
            from_person_avator = (ImageView) view.findViewById(R.id.from_person_avator);
            tv_from_msg_date = (TextView) view.findViewById(R.id.tv_from_msg_date);
        }
    }

    class FromImageVIewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_item_from_image;
        private ImageView from_person_avator;

        public FromImageVIewHolder(View view) {
            super(view);
            iv_item_from_image = (ImageView) view.findViewById(R.id.iv_from_image);
            from_person_avator = (ImageView) view.findViewById(R.id.from_person_avator);
        }
    }
}

发送者文本的布局

 

 

<?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">

    <TextView
        android:id="@+id/tv_send_msg_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="5dp"
        android:text="2015-6-6 06:06:06"
        android:textSize="12sp"
        android:visibility="gone" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:gravity="right">

        <TextView
            android:id="@+id/tv_item_send_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="110dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/pop_right"
            android:gravity="center_vertical"
            android:paddingBottom="6dp"
            android:paddingLeft="8dp"
            android:paddingRight="16dp"
            android:paddingTop="6dp"
            android:textColor="@android:color/white"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/from_person_avator02"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_toRightOf="@id/tv_item_send_txt"
            android:background="@drawable/default_icon_headphoto" />
    </LinearLayout>
</LinearLayout>

效果图

 

发送者文本效果发送者文本效果

发送者图片的布局

 

<?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">

    <TextView
        android:id="@+id/tv_send_msg_Image_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="5dp"
        android:text="2015-6-6 06:06:06"
        android:textSize="12sp"
        android:visibility="gone" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:gravity="right"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/iv_send_image"
            android:layout_width="100dp"
            android:layout_height="120dp"
            android:layout_marginRight="10dp"
            android:clickable="true"
            android:focusable="true"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="13dp"
            android:paddingTop="5dp" />

        <ImageView
            android:id="@+id/from_person_avator02"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/default_icon_headphoto"
            android:clickable="true" />
    </LinearLayout>
</LinearLayout>

效果图

 

发送者图片效果发送者图片效果

接收者文本布局

 

<?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">

    <TextView
        android:id="@+id/tv_from_msg_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="5dp"
        android:text="2015-6-6 06:06:06"
        android:textSize="12sp"
        android:visibility="gone" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">

        <ImageView
            android:id="@+id/from_person_avator"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/default_icon_headphoto" />

        <TextView
            android:id="@+id/tv_item_from_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="70dp"
            android:layout_toRightOf="@id/from_person_avator"
            android:background="@drawable/pop_left"
            android:gravity="center_vertical"
            android:paddingBottom="6dp"
            android:paddingLeft="15dp"
            android:paddingRight="8dp"
            android:paddingTop="6dp"
            android:text="你好!"
            android:textSize="16sp" />
    </RelativeLayout>
</LinearLayout>

效果图

 

接收者字体效果接收者字体效果

接收者图片布局

 

<?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">

    <TextView
        android:id="@+id/tv_from_msg_Image_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="5dp"
        android:text="2015-6-6 06:06:06"
        android:textSize="12sp"
        android:visibility="gone" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="horizontal">


        <ImageView
            android:id="@+id/from_person_avator"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/default_icon_headphoto" />

        <ImageView
            android:id="@+id/iv_from_image"
            android:layout_width="100dp"
            android:layout_height="120dp"
            android:layout_marginLeft="10dp"
            android:clickable="true"
            android:paddingBottom="5dp"
            android:paddingLeft="13dp"
            android:paddingRight="5dp"
            android:paddingTop="5dp" />
    </LinearLayout>

</LinearLayout>

效果图

 

接收者图片效果接收者图片效果

主页面的逻辑

 

/**
 * 获取当前会话所有的会话列表
 */
public void getallmessage() {
    //获取此会话的所有消息
    emMessages = conversation.getAllMessages();
    chartListVIewAdapter = new ChartListVIewAdapter(emMessages, ChatActivity.this, userAvator);
    lvChart.setLayoutManager(new LinearLayoutManager(this));
    lvChart.setAdapter(chartListVIewAdapter);
    lvChart.smoothScrollToPosition(emMessages.size());
    chartListVIewAdapter.notifyDataSetChanged();
    L.i("消息个数", emMessages.size() + "");
}

ok!完事看下效果

 

最后效果最后效果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值