博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网易云信自定义消息(类似微信分享界面)
阅读量:4516 次
发布时间:2019-06-08

本文共 4118 字,大约阅读时间需要 13 分钟。

 
首先,我们先定义一个自定义消息的类型
public interface CustomAttachmentType {
// 多端统一 int Guess = 1; int SnapChat = 2; int Sticker = 3; int RTS = 4; int SHARE = 5;//自定义 } 第二步,我们先定义一个自定义消息附件的基类,负责解析你的自定义消息的公用字段,比如类型等

注意: 实现 MsgAttachment 接口的成员都要实现 Serializable。(这个类demo里面有的,猜拳用的)

public abstract class CustomAttachment implements MsgAttachment {

    protected int type;
    CustomAttachment(int type) {
        this.type = type;
    }
    public void fromJson(JSONObject data) {
        if (data != null) {
            parseData(data);
        }
    }
    @Override
    public String toJson(boolean send) {
        return CustomAttachParser.packData(type, packData());
    }
    public int getType() {
        return type;
    }
    protected abstract void parseData(JSONObject data);
    protected abstract JSONObject packData();
}

第三步,继承这个基类,实现“分享”的附件类型。注意,成员变量都要实现 Serializable

 

public class fghGuessAttachment extends CustomAttachment {
private String url = "http://img0.imgtn.bdimg.com/it/u=1737766921,271555379&fm=21&gp=0.jpg";//这个图片没使用,在布局里面放张默认图片 private String content = "内容xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; private String title="标题xxxx"; protected int type; public fghGuessAttachment() { super(CustomAttachmentType.SHARE); } public fghGuessAttachment(String str) { this(); } @Override protected void parseData(JSONObject data) { title=data.getString("title"); content=data.getString("content"); url=data.getString("url"); } @Override public JSONObject packData() { JSONObject data = new JSONObject(); data.put("title",title); data.put("content",content); data.put("url",url); return data; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } 第四步,实现自定义消息的附件解析器。
public class CustomAttachParser implements MsgAttachmentParser {//类里面
 
case CustomAttachmentType.SHARE:     attachment = new fghGuessAttachment();     break;
 
第五步,将自定义消息展示UI上
viewholder包名下
/**  * UIKit自定义消息界面用法展示类  */ public class fghSessionHelper extends MsgViewHolderBase{
private fghGuessAttachment attachment; private ImageView image; private TextView tvTitle; private TextView tvContent; @Override protected int getContentResId() { return R.layout.aaaaaaaa; } @Override protected void inflateContentView() { image = (ImageView) findViewById(R.id.image); tvTitle = (TextView) findViewById(R.id.tv_title); tvContent = (TextView) findViewById(R.id.tv_content); } @Override protected void bindContentView() { attachment = (fghGuessAttachment)message.getAttachment(); tvTitle.setText(attachment.getTitle()); tvContent.setText(attachment.getContent()); }
//若是要自己修改气泡背景 // 当是发送出去的消息时,内容区域背景的drawable id @Override protected int rightBackground() {
return com.netease.nim.uikit.R.drawable.nim_message_item_right_selector2; }
}
第六步,发送自定义消息
public class fghGuessAction extends BaseAction {
public fghGuessAction() {
super(R.drawable.message_plus_guess_selector, R.string.input_panel_share); } @Override public void onClick() { fghGuessAttachment attachment = new fghGuessAttachment(); IMMessage message; if (getContainer() != null && getContainer().sessionType == SessionTypeEnum.ChatRoom) { message = ChatRoomMessageBuilder.createChatRoomCustomMessage(getAccount(), attachment); } else { message = MessageBuilder.createCustomMessage(getAccount(), getSessionType(), attachment); } sendMessage(message); } }
 

 

第七步,将该附件解析器注册到 SDK 中。为了保证生成历史消息时能够正确解析自定义附件,注册一般应放在 Application 的 onCreate 中完成
NIMClient.getService(MsgService.class).registerCustomAttachmentParser(new CustomAttachParser()); // 监听的注册,必须在主进程中。
第八步,注册扩展消息类型的显示ViewHolder,由于这里使用我们UIKIT,所以也需要注册到Application的onCreate中
public class SessionHelper {//这个类里面
 
//fgh NimUIKit.registerMsgItemViewHolder(fghGuessAttachment.class,fghSessionHelper.class);
第九步,在加号里面增加一个选项 ,Demo是在SessionHelper.java里面,定制的单聊界面。
/**  * UIKit自定义消息界面用法展示类  */ public class SessionHelper {
 
// 定制加号点开后可以包含的操作, 默认已经有图片,视频等消息了 ArrayList
actions = new ArrayList<>(); actions.add(new fghGuessAction());
 

 

转载于:https://www.cnblogs.com/Android-FJH/p/5690742.html

你可能感兴趣的文章
开发extjs常用的插件
查看>>
ASP.NET中Request.InputStream使用
查看>>
参数化曲面的绘制
查看>>
关于datawindow does not have update capability
查看>>
第六周学习总结
查看>>
UITableViewDataSource Protocol Reference
查看>>
hdu 1251 统计难题 (字典树入门题)
查看>>
Java-API-Package:org.springframework.web.bind.annotation
查看>>
做法:做法
查看>>
umask 文件默认权限
查看>>
JSON parse error: No suitable constructor found for type
查看>>
Luogu P4161 [SCOI2009]游戏 数论+DP
查看>>
【luogu4145】 上帝造题的七分钟2 / 花神游历各国 [线段树]
查看>>
C++笔记(2018/2/7)
查看>>
Atan2
查看>>
ThinkSNS+ 是如何计算字符显示长度的
查看>>
JSTL的使用
查看>>
c#推箱子
查看>>
随机数据生成
查看>>
linux中文输入法 ibus
查看>>