PrivateChatAdapter.java 6.41 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 java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by luxiaoman on 2017/3/18.
 */
public class PrivateChatAdapter extends BaseRecyclerAdapter<ChatBean> {
    public static final int MSG_TYPE_RECEIVE=0;//接收到的消息类型
    public static final int MSG_TYPE_SEND=1;//发送的消息类型
    public static final int MSG_TYPE_SYSTEM=2;//系统的消息类型

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

    private int tag;

    public PrivateChatAdapter(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_RECEIVE:
                viewHolder = new ChatViewReceiverHolder(LayoutInflater.from(mContext).inflate(R.layout.lv_chat_item_receive, parent, false));
                break;
            case MSG_TYPE_SEND:
                viewHolder = new ChatViewSendHolder(LayoutInflater.from(mContext).inflate(R.layout.lv_chat_item_send, 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_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_RECEIVE:
                ChatViewReceiverHolder chatViewReceiverHolder = (ChatViewReceiverHolder) holder;
                chatViewReceiverHolder.chat_header.setImageURI(Uri.parse(chatBean.getData().getUserInfo().getHeadUrl()));
                chatViewReceiverHolder.tv_chatName.setText(chatBean.getData().getUserInfo().getUserName() == null ? "" : chatBean.getData().getUserInfo().getUserName() + " : ");
                chatViewReceiverHolder.tv_chatContent.setText(chatBean.getData().getContent() == null ? "" : chatBean.getData().getContent());
                break;
            case MSG_TYPE_SEND:
                ChatViewSendHolder chatViewSendHolder = (ChatViewSendHolder) holder;
                chatViewSendHolder.chat_header.setImageURI(Uri.parse(chatBean.getData().getUserInfo().getHeadUrl()));
                chatViewSendHolder.tv_chatName.setText(chatBean.getData().getUserInfo().getUserName() == null ? "" : chatBean.getData().getUserInfo().getUserName() + " : ");
                chatViewSendHolder.tv_chatContent.setText(chatBean.getData().getContent() == null ? "" : chatBean.getData().getContent());
                break;
            case MSG_TYPE_SYSTEM:
                ChatViewSystemHolder chatViewSystemHolder=(ChatViewSystemHolder) holder;
                chatViewSystemHolder.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()));
                chatViewLandscapeHolder.tv_chatContent.setText(chatBean.getData().getContent() == null ? "" : chatBean.getData().getContent());
                break;
        }
    }

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

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


    public class ChatViewSendHolder 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 ChatViewSendHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ChatViewReceiverHolder 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 ChatViewReceiverHolder(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 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);
        }
    }
}