环信有自己的EaseUi,使用起来很是方便,其中也提供了自定义聊天界面里面的item样式修改,虽然有文档,但是初一看还是有点懵逼,这几天正好修改了界面,再次做个记录
下面是修改环信对话某一个对话(如单聊或群聊亦或其他)的自定义文本消息样式,其他类型消息修改也与此类似
我们自己可以自定义一个Fragment直接继承它原有的EaseChatFragment,然后在activity中将其添加进去即可:如下
public class NoticeEaseUiFragment extends EaseChatFragment{ @Override public void hideTitleBar() { super.hideTitleBar(); } @Override protected void initView() { super.initView(); inputMenu.setVisibility(View.GONE); hideTitleBar(); } }
NoticeEaseUiFragment noticeEaseUiFragment = new NoticeEaseUiFragment(); //传入参数 Bundle args = new Bundle(); args.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE); args.putString(EaseConstant.EXTRA_USER_ID, AllData.NOTICE_MESSAGE); noticeEaseUiFragment.setArguments(args); noticeEaseUiFragment.hideTitleBar(); noticeEaseUiFragment.setChatFragmentHelper(this); getSupportFragmentManager().beginTransaction().add(R.id.fragmentLayout,noticeEaseUiFragment).show(noticeEaseUiFragment).commit();
注意
setChatFragmentHelper(this);方法我们需要设置,,需要在activity中实现
implements EaseChatFragment.EaseChatFragmentHelper
需要这个接口,然后我们在这个接口里面的做出修改,具体每个方法的作用可以参照api文档上面的说明
/** * 设置消息扩展属性 */ @Override public void onSetMessageAttributes(EMMessage message) { message.setAttribute(EaseConstant.MESSAGE_ATTR_IS_BIG_EXPRESSION,true); }
在onSetMessageAttributes方法中,设置一个自定义类型的消息
/** * 设置自定义chatrow提供者 * @return */ @Override public EaseCustomChatRowProvider onSetCustomChatRowProvider() { return new CustomChatRowProvider(this); }
在onSetCustomChatRowProvider方法中设置chatrow提供者,CustomChatRowProvider为自己自定义。如下:
public class CustomChatRowProvider implements EaseCustomChatRowProvider { private Context mContext; public CustomChatRowProvider(Context context){ this.mContext = context; } @Override public int getCustomChatRowTypeCount() { return 4; } @Override public int getCustomChatRowType(EMMessage message) { return 0; } @Override public EaseChatRow getCustomChatRow(EMMessage message, int position, BaseAdapter adapter) { return new EaseNoticeText(mContext,message,position,adapter); } }
主要是getCustomChatRow方法,这里面直接返回一个EaseNoticeText对象,此类便是我们自定义的样式类了,看环信原来的代码可以发现,它所有的类型UI都是继承于EascChatRow类,所有我们自定义的类也直接继承EascChatRow类即可,如下:
public class EaseNoticeText extends EaseChatRow
继承了该类,实现其中几个方法:
@Override protected void onInflateView() { inflater.inflate(R.layout.view_ease_nitice, this); }
该方法为我们修改的item样式布局
@Override protected void onFindViewById() { time = (TextView)findViewById(R.id.time); contentText=(TextView)findViewById(R.id.content); }
获取其中的控件
@Override protected void onUpdateView() { adapter.notifyDataSetChanged(); }这个直接写就可以
@Override protected void onSetUpView() {}
最后我们在这里面赋值即可,所有的内容在message对象里面,可以获取其中的body或者自定义的扩展消息属性,赋值给控件即可
@Override protected void onBubbleClick() { }
这个方法可以不做改动
最后我们找到它里面的一个类:
EaseMessageAdapter
找到这个类之后,在下面找到
createChatRow方法
找到这个方法之后,因为我只需要修改文本消息的样式,所以只需要在TXT类型里面进行判断,如果需要修改其他样式,可以扩展其他样式。 这里面用蓝色线条画出来的,就是最上面实现了
EaseChatFragment.EaseChatFragmentHelper
实现了这个接口的设置扩展属性的方法里面里面的设置值
以上便是修改某个对话的文本的自定义样式了,如果需要修改所有对话item样式,可以直接去它原来的ui布局文件里面修改