基于 MultiType(RecyclerView多类型适配器)

1
implementation 'me.drakeet.multitype:multitype:3.5.0'

创建含有type的Bean类(type可替换为具有区分度的参数)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class ChatMsg {

private String msg;
private int type;

public void setMsg(String msg) {
this.msg = msg;
}
public String getMsg() {
return msg;
}

public void setType(int type) {
this.type = type;
}
public int getType() {
return type;
}

}

然后根据Bean类中的type值来区分布局模式

1
2
3
4
5
6
7
8
9
10
11
12
public class ChatRoomBinderLinker implements ClassLinker<ChatMsg> {
@NonNull
@Override
public Class<? extends ItemViewBinder<ChatMsg, ?>> index(int position, @NonNull ChatMsg chatMsg) {
if (chatMsg.getType() == 1) {
return ChatRoomMessageBinder.class;
} else if(chatMsg.getType() == 2) {
return ChatRoomWelcomeBinder.class;
}
return ChatRoomEmptyBinder.class;
}
}