关于android:自定义消息-包含-list-数组

5次阅读

共计 6213 个字符,预计需要花费 16 分钟才能阅读完成。

自定义音讯 蕴含 list 数组

公司产品越来越简单,业务线也一直的减少,我的项目中聊天模块应用了融云的 sdk,然而 sdk 中自带的音讯类型无限,不能满足产品须要的多张图片布局的音讯类型,只能自定义一个,然而发现没有蕴含 list 的音讯例子,实现起来有点繁琐,这边给大家参考下。
——– 音讯体 ———-

  1. 新建一自定义音讯类,继承 MessageContent

2. 实现 encode() 办法,该办法的性能是将音讯属性封装成 json 串,再将 json 串转成 byte 数组,该办法会在发消息时调用
留神:要在这个办法外面加上这句话用来携带用户信息
if (getJSONUserInfo() != null){

            jsonObj.putOpt("user", getJSONUserInfo());
        }

3. 笼罩父类的 MessageContent(byte[] data) 构造方法,该办法将对收到的音讯进行解析,先由 byte 转成 json 字符串,再将 json 中内容取出赋值给音讯属性。
留神:要在这个办法外面加上这句话用来解析携带用户信息
if (jsonObj.has(“user”))

            setUserInfo(parseJsonToUserInfo(jsonObj.getJSONObject("user")));

4.MessageContent 已实现 Parcelable 接口,自定义音讯类也须要实现 Parcelable

5. 减少注解信息:注解名:MessageTag;属性:value,flag;value 即 ObjectName 是音讯的惟一标识不能够反复,
开发者命名时不能以 RC 结尾,防止和融云内置音讯抵触;flag 是用来定义音讯的可操作状态。
flag 值如下表:
枚举值 阐明
MessageTag.NONE 为空值,不示意任何意义,发送的自定义音讯不会在会话页面和会话列表中展现。
MessageTag.ISCOUNTED 示意客户端收到音讯后,要进行未读音讯计数(未读音讯数减少 1),所有内容型音讯都应该设置此值。非内容类音讯暂不反对音讯计数。
MessageTag.ISPERSISTED 示意客户端收到音讯后,要进行存储,并在之后能够通过接口查问,存储后会在会话界面中显示。
MessageTag.STATUS 在本地不存储,不计入未读数,并且如果对方不在线,服务器会间接抛弃该音讯,对方如果之后再上线也不会再收到此音讯(聊天室类型除外,此类音讯聊天室会视为一般音讯)。

6. 自定义音讯应在 init 后注册 RongIM.registerMessageType(CustomizeMessage.class);
———– 自定义音讯展现 ———

package cn.rongcloud.demo;

import android.os.Parcel;

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

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import io.rong.common.ParcelUtils;
import io.rong.common.RLog;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

@MessageTag(value = "sendGoodsLocal", flag = MessageTag.ISPERSISTED)
public class SendGoodsLocalMessage extends MessageContent {
    private String content;
    private String id;
    private int typeid;
    private List<String> img =new ArrayList<>();
    private SendGoodsLocalMessage() {}

    public SendGoodsLocalMessage(Parcel in) {this.content = ParcelUtils.readFromParcel(in);
        this.id = ParcelUtils.readFromParcel(in);
        this.typeid = ParcelUtils.readIntFromParcel(in);
        this.img = ParcelUtils.readListFromParcel(in,String.class);
    }

    public SendGoodsLocalMessage(byte[] data) {
        String jsonStr = null;
        try {jsonStr = new String(data, "UTF-8");
        } catch (UnsupportedEncodingException var5) {RLog.e("GroupNotificationMessage", "UnsupportedEncodingException", var5);
        }

        try {JSONObject jsonObj = new JSONObject(jsonStr);
            this.setContent(jsonObj.optString("content"));
            this.setId(jsonObj.optString("id"));
            this.setTypeid(jsonObj.optInt("typeid"));
            JSONArray jsonArray = jsonObj.optJSONArray("img");
            List<String> imgList = new ArrayList<>();
            for (int i = 0; i < jsonArray.length(); i++) {imgList.add((String) jsonArray.get(i));
            }
            setImg(imgList);
//            this.setImg();} catch (JSONException var4) {RLog.e("GroupNotificationMessage", "JSONException" + var4.getMessage());
        }

    }

    public static final Creator<SendGoodsLocalMessage> CREATOR = new Creator<SendGoodsLocalMessage>() {public SendGoodsLocalMessage createFromParcel(Parcel source) {return new SendGoodsLocalMessage(source);
        }

        public SendGoodsLocalMessage[] newArray(int size) {return new SendGoodsLocalMessage[size];
        }
    };

    public static SendGoodsLocalMessage obtain(String content, String id, int typeid, List<String> img) {SendGoodsLocalMessage obj = new SendGoodsLocalMessage();
        obj.content = content;
        obj.id = id;
        obj.typeid = typeid;
        obj.img = img;
        return obj;
    }

    public String getContent() {return content;}

    public List<String> getImg() {return img;}

    public void setImg(List<String> img) {this.img = img;}

    public int getTypeid() {return typeid;}

    public String getId() {return id;}

    public void setContent(String content) {this.content = content;}

    public void setId(String id) {this.id = id;}

    public void setTypeid(int typeid) {this.typeid = typeid;}

    @Override
    public byte[] encode() {JSONObject jsonObj = new JSONObject();
        try {jsonObj.put("content", this.content);
            jsonObj.put("id", this.id);
            jsonObj.put("typeid", this.typeid);

            JSONArray jsonArray = new JSONArray();
            for (String name : img) {jsonArray.put(name);
            }
            jsonObj.put("img", jsonArray);

        } catch (JSONException var4) {RLog.e("GroupNotificationMessage", "JSONException" + var4.getMessage());
        }
        try {return jsonObj.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException var3) {RLog.e("GroupNotificationMessage", "UnsupportedEncodingException", var3);
            return null;
        }
    }

    @Override
    public int describeContents() {return 0;}

    @Override
    public void writeToParcel(Parcel dest, int flags) {ParcelUtils.writeToParcel(dest, this.content);
        ParcelUtils.writeToParcel(dest, this.id);
        ParcelUtils.writeToParcel(dest, this.typeid);
        ParcelUtils.writeToParcel(dest, this.img);
    }
}
package cn.rongcloud.demo;


import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.bumptech.glide.Glide;

import io.rong.imkit.model.ProviderTag;
import io.rong.imkit.model.UIMessage;
import io.rong.imkit.widget.provider.IContainerItemProvider;

@ProviderTag(
        messageContent = SendGoodsLocalMessage.class,
        showPortrait = false,
        centerInHorizontal = true,
        showProgress = false,
        showSummaryWithName = false
)
public class SendGoodsLocalProvider extends IContainerItemProvider.MessageProvider<SendGoodsLocalMessage> {

    @Override
    public void bindView(View v, int i, SendGoodsLocalMessage mSendGoodsLocalMessage, UIMessage uiMessage) {ViewHolder holder = (ViewHolder) v.getTag();
        holder.tvGoodsName.setText(mSendGoodsLocalMessage.getContent());
        Glide.with(v.getContext()).load(mSendGoodsLocalMessage.getImg().get(0)).centerCrop().into(holder.ivCover);
    }

    @Override
    public Spannable getContentSummary(Context context, SendGoodsLocalMessage message) {if (message == null) {return null;}
        return new SpannableString(message.getContent());
    }


    @Override
    public String getPushContent(Context context, UIMessage message) {return super.getPushContent(context, message);
    }

    @Override
    public Spannable getContentSummary(SendGoodsLocalMessage mSendGoodsLocalMessage) {return null;}

    @Override
    public void onItemClick(View view, int i, SendGoodsLocalMessage mSendGoodsLocalMessage, UIMessage uiMessage) { }

    @Override
    public View newView(Context context, ViewGroup viewGroup) {View view = LayoutInflater.from(context).inflate(R.layout.rc_send_goods_message, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.tvGoodsName = view.findViewById(R.id.tvGoodsName);
        viewHolder.tvSend = view.findViewById(R.id.tvSend);
        viewHolder.ivCover = view.findViewById(R.id.ivCover);
        view.setTag(viewHolder);
        return view;
    }
    private static class ViewHolder {
        TextView tvGoodsName,tvSend;
        ImageView ivCover;
        private ViewHolder() {}
    }
}

大家能够当一个参考。

正文完
 0