ChatRoomAdapter.java 6.29 KB
package com.cnlive.goldenline.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cnlive.goldenline.R;
import com.cnlive.goldenline.model.ChatBean;
import com.cnlive.goldenline.ui.base.BaseRecyclerAdapter;
import com.facebook.drawee.view.SimpleDraweeView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by luxiaoman on 2017/3/16.
 */
public class ChatRoomAdapter extends BaseRecyclerAdapter<ChatBean> {
    public static final int MSG_TYPE_COMMON=0;//接收到的消息类型
    public static final int MSG_TYPE_SYSTEM=1;//系统的消息类型
    public static final int MSG_TYPE_GIFT=2;  //礼物的消息类型

    public static final int MSG_TYPE_LANDSCAPE=3;  //横屏的消息类型

    private int tag;

    public ChatRoomAdapter(Context context, List<ChatBean> msgList) {
        super(context);
        updateItems(msgList);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case MSG_TYPE_COMMON:
                viewHolder = new ChatViewCommonHolder(LayoutInflater.from(mContext).inflate(R.layout.lv_chat_item_receive, parent, false));
                break;
            case MSG_TYPE_SYSTEM:
                viewHolder = new ChatViewSystemHolder(LayoutInflater.from(mContext).inflate(R.layout.lv_chat_item_system, parent, false));
                break;
            case MSG_TYPE_GIFT:
                viewHolder = new ChatViewGiftHolder(LayoutInflater.from(mContext).inflate(R.layout.lv_chat_item_system, parent, false));
                break;
            case MSG_TYPE_LANDSCAPE:
                viewHolder = new ChatViewLandscapeHolder(LayoutInflater.from(mContext).inflate(R.layout.lv_chat_item_landscape, parent, false));
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ChatBean chatBean = mItems.get(position);
        switch (getItemViewType(position)) {
            case MSG_TYPE_COMMON:
                ChatViewCommonHolder chatViewCommonHolder = (ChatViewCommonHolder) holder;
                chatViewCommonHolder.chat_header.setImageURI(Uri.parse(chatBean.getData().getUserInfo().getHeadUrl()));
                chatViewCommonHolder.tv_chatName.setText(chatBean.getData().getUserInfo().getUserName() == null ? "" : chatBean.getData().getUserInfo().getUserName() + " : ");
                try {
                    chatViewCommonHolder.tv_chatContent.setText(chatBean.getData().getContent() == null ? "" : (String) new JSONObject(chatBean.getData().getContent()).get("message"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case MSG_TYPE_SYSTEM:
                ChatViewSystemHolder chatViewSystemHolder=(ChatViewSystemHolder) holder;
                chatViewSystemHolder.chat_system.setText(chatBean.getData().getUserInfo().getUserName()+"成功加入聊天大厅");
                break;
            case MSG_TYPE_GIFT:
                ChatViewGiftHolder chatViewGiftHolder=(ChatViewGiftHolder) holder;
                chatViewGiftHolder.chat_system.setText(chatBean.getData().getUserInfo().getUserName()+"赠送礼物");
                break;
            case MSG_TYPE_LANDSCAPE:
                ChatViewLandscapeHolder chatViewLandscapeHolder=(ChatViewLandscapeHolder) holder;
                chatViewLandscapeHolder.chat_header.setImageURI(Uri.parse(chatBean.getData().getUserInfo().getHeadUrl()));
                try {
                    chatViewLandscapeHolder.tv_chatContent.setText(chatBean.getData().getContent() == null ? "" : (String) new JSONObject(chatBean.getData().getContent()).get("message"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;

        }
    }

    @Override
    public int getItemViewType(int position) {
        String type = getItem(position).getType();
        if ("0".equals(type)) {
            tag = MSG_TYPE_COMMON;
        } else if ("1".equals(type)) {
            tag = MSG_TYPE_SYSTEM;
        }else if ("2".equals(type)) {
            tag = MSG_TYPE_GIFT;
        }else if ("3".equals(type)) {
            tag = MSG_TYPE_LANDSCAPE;
        }
        return tag;
    }

    @Override
    public int getItemCount() {
        return mItems == null ? 0 : mItems.size();
    }

    public class ChatViewCommonHolder extends RecyclerView.ViewHolder {

        //聊天头像
        @BindView(R.id.chat_header)
        SimpleDraweeView chat_header;

        //聊天姓名
        @BindView(R.id.tv_chatName)
        TextView tv_chatName;

        //聊天内容
        @BindView(R.id.tv_chatContent)
        TextView tv_chatContent;

        public ChatViewCommonHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ChatViewSystemHolder extends RecyclerView.ViewHolder {

        //聊天系统消息
        @BindView(R.id.chat_system)
        TextView chat_system;

        public ChatViewSystemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ChatViewGiftHolder extends RecyclerView.ViewHolder {

        //送礼物系统消息
        @BindView(R.id.chat_system)
        TextView chat_system;

        public ChatViewGiftHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ChatViewLandscapeHolder extends RecyclerView.ViewHolder {
        //横屏聊天消息
        //聊天头像
        @BindView(R.id.chat_header)
        SimpleDraweeView chat_header;

        //聊天内容
        @BindView(R.id.tv_chatContent)
        TextView tv_chatContent;

        public ChatViewLandscapeHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}