自定义处理会话列表数据
自定义处理会话列表数据
数据处理机制支持对会话列表数据进行以下自定义处理:
- 按会话类型过滤会话,被过滤掉的会话不在会话列表展示。
- 按自定义规则过滤会话,会话列表页面仅展示过滤后的数据。
- 设置某类型会话聚合显示。
数据处理器(抽象类)
数据处理器通过抽象类的形式提供,支持的版本为 5.1.3.x 系列中的稳定版、5.1.5 及之后所有版本。其它版本的数据处理机制请参考数据处理接口部分的说明。
数据处理器说明
名称:BaseDataProcessor
该抽象类默认提供了以下三个方法,开发者可以根据业务需要覆写任意方法达到自定义需求。
返回值 | 方法 |
---|---|
Conversation.ConversationType[] | supportedTypes() |
List<T> | filtered(List<T> data) |
boolean | isGathered(Conversation.ConversationType type) |
boolean | isGathered(ConversationIdentifier identifier) |
方法说明
supportedTypes()
会话列表页支持的会话类型数组, 默认支持所有类型的会话。
filtered(List<T> data)
对会话数据进行过滤,当从数据库批量拉取到会话和在线收到消息产生新会话时会回调此方法。
参数 类型 说明 data List<T> 待过滤的数据 isGathered(Conversation.ConversationType type)
设置某一会话类型是否聚合显示,如果需要聚合显示,返回 true, 否则返回 false.
返回 true 后,该类型的所有会话在会话列表将显示为一条,点击此聚合会话将跳转到聚合会话列表,此时才展示该类型的所有会话,详情参考按类型聚合会话。
isGathered(ConversationIdentifier identifier)
参数 类型 说明 identifier ConversationIdentifier (SDK 5.3.0 版本新增默认方法) 包含 Conversation.ConversationType、targetId(String) 。 (DataProcessor 接口的 default 方法)设置某一会话类型是否聚合显示,如果需要聚合显示,返回 true, 否则返回 false.
返回 true 后,该类型的所有会话在会话列表将显示为一条,点击此聚合会话将跳转到聚合会话列表,此时才展示该类型的所有会话,详情参考按类型聚合会话。
自定义数据处理
自定义数据处理器。声明自定义的数据处理器类,继承 SDK 的
BaseDataProcessor
, 复写需要自定义的方法。下面以自定义数据处理类
MyDataProcessor
为例说明:public class MyDataProcessor extends BaseDataProcessor<Conversation> { /** * 自定义会话列表页面支持的会话类型,此处设置为仅支持单聊和群组会话。 */ @Override public Conversation.ConversationType[] supportedTypes() { Conversation.ConversationType[] types = {Conversation.ConversationType.PRIVATE, Conversation.ConversationType.GROUP}; return types; } /** * 自定义需要聚合显示的会话。此处设置系统会话聚合显示。 */ @Override public boolean isGathered(Conversation.ConversationType type) { if (type.equals(Conversation.ConversationType.SYSTEM)) { return true; } else { return false; } } /** * 自定义需要聚合显示的会话。此处设置系统会话聚合显示。 */ @Override public boolean isGathered(ConversationIdentifier identifier) { if (identifier.getType().equals(Conversation.ConversationType.SYSTEM)) { return true; } else { return false; } } }
已复制1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34设置数据处理器。在打开会话列表页面之前,调用下面方法设置自定义的数据处理器。
RongConfigCenter.conversationListConfig().setDataProcessor(new MyDataProcessor());
已复制1上面示例代码里的
MyDataProcessor
为第 1 步的自定义数据处理器。
数据处理接口
SDK 5.1.5 之前的开发版,可通过数据处理接口进行会话拦截。
通过 Android Studio 实现数据处理接口时,部分方法的默认返回值为 null, 会导致会话列表的数据全部被过滤,显示空白会话列表。请务必注意修改返回值为自定义处理后的数据或者原始数据。
强烈建议您升级到稳定版,通过数据处理器实现。
接口说明
数据处理接口提供以下三种方法:
public interface DataProcessor<T> {
/**
* 设置会话列表页支持的会话类型
* @return 所支持的会话类型
*/
Conversation.ConversationType[] supportedTypes();
/**
* 对会话数据进行过滤。
* <p>从数据库批量拉取到会话列表时和在线收到消息产生新会话时都会回调此方法</p>
* @param data 待过滤的数据
* @return 过滤完的数据。
*/
List<T> filtered(List<T> data);
/**
* 某一会话类型是否聚合状态显示。
* @param type 会话类型
*/
boolean isGathered(Conversation.ConversationType type);
/**
* 某一会话类型是否聚合状态显示。
*
* @param identifier 会话标识符
*/
default boolean isGathered(ConversationIdentifier identifier) {
if (identifier == null || identifier.getType() == null) {
return false;
}
return isGathered(identifier.getType());
}
}
已复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
自定义数据处理
声明自定义的数据处理类,实现
DataProcessor
接口, 按照自定义需求实现各方法。private Conversation.ConversationType[] supportedTypes = {Conversation.ConversationType.PRIVATE, Conversation.ConversationType.GROUP, Conversation.ConversationType.SYSTEM}; public class MyDataProcessor implements DataProcessor<Conversation> { @Override public Conversation.ConversationType[] supportedTypes() { //返回自定义的会话列表所支持的会话类型。 return supportedTypes; } @Override public List<Conversation> filtered(List<Conversation> data) { //返回过滤后的数据,此处示例没有过滤,返回原始数据。 return data; //请注意不要返回 null !!!! } @Override public boolean isGathered(Conversation.ConversationType type) { //自定义系统会话聚合显示,其它会话非聚合。 if (type.equals(Conversation.ConversationType.SYSTEM)) { return true; } return false; } @Override public boolean isGathered(ConversationIdentifier identifier) { //自定义系统会话聚合显示,其它会话非聚合。 if (identifier.getType().equals(Conversation.ConversationType.SYSTEM)) { return true; } return false; } }
已复制1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34通过下面的方法将自定义的数据处理类设置到 SDK 里。
RongConfigCenter.conversationListConfig().setDataProcessor(new MyDataProcessor());
已复制1