美文网首页
自定义融云引用信息UI

自定义融云引用信息UI

作者: 你的益达233 | 来源:发表于2020-08-20 17:35 被阅读0次

自定义融云引用信息UI

这里主要分享个思路,原来是自己写的,发现要处理的情况太多,而且容易出问题。
然后就想融云它本身已经有了,我只是改变ui样式而言,它所有的有关界面的东西都在IMKit里面,然后就去IMKit.jar里面找,果然在里面找到ReferenceMessageItemProvider。
看到这你应该懂了
慢慢参考ReferenceMessageItemProvider,其实更多的是复制代码过来
提供下我项目的CustomizeReferenceMsgItemProvider

@ProviderTag(
    messageContent = ReferenceMessage.class,
    showProgress = false,
    showReadState = true
)
public class CustomizeReferenceMsgItemProvider extends IContainerItemProvider.MessageProvider<ReferenceMessage> {

@Override
public void bindView(View view, int position, ReferenceMessage referenceMessage, UIMessage uiMessage) {

    ViewHolder vh = (ViewHolder) view.getTag();
    vh.referenceName.setText("");
    vh.referenceContent.setText("");
    vh.referenceSendContent.setText("");
    if (uiMessage.getMessageDirection() == Message.MessageDirection.SEND){
        //发送方
        vh.ll_user_send.setBackgroundResource(R.drawable.rc_ic_bubble_right);
        LinearLayout.LayoutParams llSendParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        llSendParams.gravity = Gravity.RIGHT;
        vh.ll_user_send.setLayoutParams(llSendParams);
        LinearLayout.LayoutParams llReferParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        llReferParams.gravity = Gravity.RIGHT;
        llReferParams.topMargin = UtilHelper.INSTANCE.dip2px(view.getContext(),10f);
        vh.ll_refer_msg.setLayoutParams(llReferParams);
    } else {
        //接收方
        vh.ll_user_send.setBackgroundResource(R.drawable.rc_ic_bubble_left);
        LinearLayout.LayoutParams llSendParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        llSendParams.gravity = Gravity.LEFT;
        vh.ll_user_send.setLayoutParams(llSendParams);
        LinearLayout.LayoutParams llReferParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        llReferParams.gravity = Gravity.LEFT;
        llReferParams.topMargin = UtilHelper.INSTANCE.dip2px(view.getContext(),10f);
        vh.ll_refer_msg.setLayoutParams(llReferParams);
    }
    if (referenceMessage.getUserId() != null){
        //引用的用户名
        vh.referenceName.setText(this.getDisplayName(uiMessage,referenceMessage.getUserId())+" :");
    }
    if (referenceMessage.getEditSendText() != null){
        //发送的内容
        vh.referenceSendContent.setText(referenceMessage.getEditSendText());
        SpannableStringBuilder span = this.createSpan(referenceMessage.getEditSendText());
        this.setTextContent(view, vh.referenceSendContent, span);
        this.setMovementMethod(view, uiMessage, vh.referenceSendContent);
    }
    //处理引用的信息
    if (referenceMessage.getReferenceContent() != null){
        vh.ll_refer_msg.setVisibility(View.VISIBLE);
        if (referenceMessage.getReferenceContent() instanceof TextMessage){
            setTextType(view,vh,position,referenceMessage,uiMessage);
            setMovementMethod(view,uiMessage,vh.referenceContent);
        } else if (referenceMessage.getReferenceContent() instanceof ImageMessage){
            setImageType(view,vh,position,referenceMessage,uiMessage);
        } else if (referenceMessage.getReferenceContent() instanceof FileMessage){
            setFileType(view,vh,position,referenceMessage,uiMessage);
        } else if (referenceMessage.getReferenceContent() instanceof RichContentMessage){
            setRichType(view,vh,position,referenceMessage,uiMessage);
        } else if (referenceMessage.getReferenceContent() instanceof ReferenceMessage){
            setReferenceType(view,vh,position,referenceMessage,uiMessage);
        } else {
            vh.referenceSendContent.setText(R.string.rc_message_unknown);
            vh.ll_refer_msg.setVisibility(View.GONE);
        }

    }

    ShapeUtils.INSTANCE.setShapeCorner2Color(vh.ll_refer_msg, R.color.alpha_50_c_e3e2e6, UtilHelper.INSTANCE.dip2px(view.getContext(), 2f));
}

@Override
public Spannable getContentSummary(ReferenceMessage referenceMessage) {
    return new SpannableString("引用消息");
}

@Override
public void onItemClick(View view, int i, ReferenceMessage referenceMessage, UIMessage uiMessage) {

}

@Override
public View newView(Context context, ViewGroup viewGroup) {
    View view = LayoutInflater.from(context).inflate(R.layout.im_customize_refer_msg_item, (ViewGroup) null);
    ViewHolder viewHolder = new ViewHolder();
    viewHolder.ll_user_send = view.findViewById(R.id.ll_user_send);
    viewHolder.referenceSendContent = view.findViewById(R.id.rc_msg_tv_reference_send_content);
    viewHolder.referenceContent = view.findViewById(R.id.rc_msg_tv_reference_content);
    viewHolder.ll_refer_msg = view.findViewById(R.id.ll_refer_msg);
    viewHolder.referenceName = view.findViewById(R.id.rc_msg_tv_reference_name);
    view.setTag(viewHolder);
    return view;

}

class ViewHolder {
    LinearLayout ll_user_send;
    ReferenceAutoLinkTextView referenceContent;
    ReferenceAutoLinkTextView referenceSendContent;
    LinearLayout ll_refer_msg;
    TextView referenceName;

}

private String getDisplayName(UIMessage uiMessage, String referenceUserId) {
    if (uiMessage.getSenderUserId() != null) {
        if (uiMessage.getConversationType().equals(Conversation.ConversationType.GROUP)) {
            GroupUserInfo groupUserInfo = RongUserInfoManager.getInstance().getGroupUserInfo(uiMessage.getTargetId(), referenceUserId);
            if (groupUserInfo != null) {
                return groupUserInfo.getNickname();
            }
        }

        UserInfo userInfo = RongUserInfoManager.getInstance().getUserInfo(referenceUserId);
        if (userInfo != null) {
            return userInfo.getName();
        }
    }

    return null;
}

private SpannableStringBuilder createSpan(String content) {
    SpannableStringBuilder spannable = new SpannableStringBuilder(content);
    AndroidEmoji.ensure(spannable);
    return spannable;
}

private void setTextContent(View view, final TextView textView, final SpannableStringBuilder spannable) {
    int len = spannable.length();
    if (view.getHandler() != null && len > 500) {
        view.getHandler().postDelayed(new Runnable() {
            public void run() {
                textView.setText(spannable);
            }
        }, 50L);
    } else {
        textView.setText(spannable);
    }

}

private void setMovementMethod(final View view, final UIMessage uiMessage, ReferenceAutoLinkTextView referenceContent) {
    referenceContent.setMovementMethod(new LinkTextViewMovementMethod(new ILinkClickListener() {
        public boolean onLinkClick(String link) {
            RongIM.ConversationBehaviorListener listener = RongContext.getInstance().getConversationBehaviorListener();
            RongIM.ConversationClickListener clickListener = RongContext.getInstance().getConversationClickListener();
            boolean result = false;
            if (listener != null) {
                result = listener.onMessageLinkClick(view.getContext(), link);
            } else if (clickListener != null) {
                result = clickListener.onMessageLinkClick(view.getContext(), link, uiMessage.getMessage());
            }

            if (listener == null && clickListener == null || !result) {
                String str = link.toLowerCase();
                if (str.startsWith("http") || str.startsWith("https")) {
                    Intent intent = new Intent("io.rong.imkit.intent.action.webview");
                    intent.setPackage(view.getContext().getPackageName());
                    intent.putExtra("url", link);
                    view.getContext().startActivity(intent);
                    result = true;
                }
            }

            return result;
        }
    }));
    referenceContent.stripUnderlines();
}

//引用文本类型
private void setTextType(View view, ViewHolder holder, int position, ReferenceMessage referenceMessage, UIMessage uiMessage) {

    if (referenceMessage != null && referenceMessage.getReferenceContent() != null) {
        TextMessage content = (TextMessage)referenceMessage.getReferenceContent();
        LogUtil.INSTANCE.i("setTextType",content.getContent());
        SpannableStringBuilder span = this.createSpan(content.getContent());
        this.setTextContent(view, holder.referenceContent, span);
        this.setReferenceContentAction(view, holder, position, referenceMessage, uiMessage);
        holder.referenceContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(Constant.IMTextPreviewActivity)
                        .withString("content",content.getContent())
                        .navigation();
            }
        });
        holder.ll_refer_msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(Constant.IMTextPreviewActivity)
                        .withString("content",content.getContent())
                        .navigation();
            }
        });
    }
}

//引用图片类型
private void setImageType(final View view, ViewHolder holder, int position, ReferenceMessage referenceMessage, final UIMessage uiMessage) {
    //holder.referenceContent.setOnClickListener((View.OnClickListener)null);
    if (referenceMessage != null && referenceMessage.getReferenceContent() != null) {
        ImageMessage content = (ImageMessage)referenceMessage.getReferenceContent();
        Uri imageUri = null;
        if (content.getThumUri() != null) {
            imageUri = content.getThumUri();
        } else if (content.getLocalPath() != null) {
            imageUri = content.getLocalPath();
        } else if (content.getMediaUrl() != null) {
            imageUri = content.getMediaUrl();
        }

        if (imageUri != null) {
            holder.referenceContent.setText("[图片]");

        }

        holder.ll_refer_msg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                try {
                    Intent intent = new Intent(view.getContext(), PicturePagerActivity.class);
                    intent.setPackage(view.getContext().getPackageName());
                    intent.putExtra("message", uiMessage.getMessage());
                    view.getContext().startActivity(intent);
                } catch (Exception var3) {
                    RLog.e("ReferenceMessageItemProvider", "setImageType", var3);
                }

            }
        });
        holder.ll_refer_msg.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
                return view.performLongClick();
            }
        });
        holder.referenceSendContent.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
                return view.performLongClick();
            }
        });
    }
}

//引用文件类型
private void setFileType(final View view, ViewHolder holder, int position, ReferenceMessage referenceMessage, final UIMessage uiMessage) {

    if (referenceMessage != null && referenceMessage.getReferenceContent() != null) {

        FileMessage content = (FileMessage)referenceMessage.getReferenceContent();
        String string = view.getContext().getString(R.string.rc_search_file_prefix) + ' ' + content.getName();
        SpannableStringBuilder ssb = new SpannableStringBuilder(string);
        ssb.setSpan(new ForegroundColorSpan(view.getContext().getResources().getColor(R.color.rc_reference_text_link_color)), 0, string.length(), 33);
        AndroidEmoji.ensure(ssb);
        holder.referenceContent.setText(ssb);
        holder.referenceContent.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                try {
                    Intent intent = new Intent();
                    intent.setClass(view.getContext(), FilePreviewActivity.class);
                    intent.setPackage(view.getContext().getPackageName());
                    ReferenceMessage referenceMessage = (ReferenceMessage)uiMessage.getMessage().getContent();
                    FileMessage fileMessage = (FileMessage)referenceMessage.getReferenceContent();
                    intent.putExtra("FileMessage", fileMessage);
                    intent.putExtra("Message", uiMessage.getMessage());
                    intent.putExtra("Progress", uiMessage.getProgress());
                    view.getContext().startActivity(intent);
                } catch (Exception var5) {
                    RLog.e("ReferenceMessageItemProvider", "exception: " + var5);
                }

            }
        });
        holder.ll_refer_msg.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
                return view.performLongClick();
            }
        });
        holder.referenceContent.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
                return view.performLongClick();
            }
        });
        holder.referenceSendContent.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
                return view.performLongClick();
            }
        });
    }
}

//引用富文本,如表情
private void setRichType(final View view, ViewHolder holder, int position, ReferenceMessage referenceMessage, UIMessage uiMessage) {
    holder.referenceContent.setOnClickListener((View.OnClickListener)null);
    if (referenceMessage != null && referenceMessage.getReferenceContent() != null) {
        holder.referenceContent.setOnClickListener((View.OnClickListener)null);
        final RichContentMessage content = (RichContentMessage)referenceMessage.getReferenceContent();
        String string = view.getContext().getString(R.string.rc_reference_link) + ' ' + content.getTitle();
        SpannableStringBuilder ssb = new SpannableStringBuilder(string);
        ssb.setSpan(new ForegroundColorSpan(view.getContext().getResources().getColor(R.color.rc_reference_text_link_color)), 0, string.length(), 33);
        AndroidEmoji.ensure(ssb);
        this.setTextContent(view, holder.referenceContent, ssb);
        holder.referenceContent.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                try {
                    Intent intent = new Intent(view.getContext(), RongWebviewActivity.class);
                    intent.setPackage(view.getContext().getPackageName());
                    intent.putExtra("url", content.getUrl());
                    view.getContext().startActivity(intent);
                } catch (Exception var3) {
                    RLog.e("ReferenceMessageItemProvider", "exception: " + var3);
                }

            }
        });
        holder.referenceContent.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
                return view.performLongClick();
            }
        });
        holder.referenceSendContent.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
                return view.performLongClick();
            }
        });
    }
}

//引用引用类型
private void setReferenceType(View view, ViewHolder holder, int position, ReferenceMessage referenceMessage, UIMessage uiMessage) {
    holder.referenceContent.setOnClickListener((View.OnClickListener)null);
    if (referenceMessage != null && referenceMessage.getReferenceContent() != null) {
        ReferenceMessage content = (ReferenceMessage)referenceMessage.getReferenceContent();
        SpannableStringBuilder span = this.createSpan(content.getEditSendText());
        this.setTextContent(view, holder.referenceContent, span);
        this.setReferenceContentAction(view, holder, position, referenceMessage, uiMessage);
    }
}

private void setReferenceContentAction(final View view, ViewHolder holder, int position, ReferenceMessage referenceMessage, UIMessage uiMessage) {
    holder.ll_refer_msg.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            return view.performLongClick();
        }
    });
    holder.referenceContent.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            return view.performLongClick();
        }
    });
    holder.referenceSendContent.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            return view.performLongClick();
        }
    });
    holder.referenceContent.setMovementMethod(new LinkTextViewMovementMethod(new ILinkClickListener() {
        public boolean onLinkClick(String link) {
            boolean result = false;
            String str = link.toLowerCase();
            if (str.startsWith("http") || str.startsWith("https")) {
                Intent intent = new Intent("io.rong.imkit.intent.action.webview");
                intent.setPackage(view.getContext().getPackageName());
                intent.putExtra("url", link);
                view.getContext().startActivity(intent);
                result = true;
            }

            return result;
        }
    }));
}

}

相关文章

网友评论

      本文标题:自定义融云引用信息UI

      本文链接:https://www.haomeiwen.com/subject/hjlydktx.html