BarrageInputView
)和消息列表视图(BarrageStreamView
):BarrageInputView
:点击后可以拉起输入界面BarrageStreamView
:用于展示弹幕消息列表BarrageInputView
,点击后可以拉起输入界面:// 文件位置:tuilivekit/src/main/java/com/trtc/uikit/livekit/component/barrage/BarrageInputView
.javaBarrageInputView
barrageInputView = newBarrageInputView
(mContext, roomId);mBarrageInput
Container.addView(barrageInputView);
BarrageInputView
,点击后弹出输入面板(右侧图红色框): |
BarrageStreamView
来展示弹幕消息:// 文件位置:tuilivekit/src/main/java/com/trtc/uikit/livekit/component/barrage/BarrageStreamView
.javaBarrageStreamView
barrageStreamView
= newBarrageStreamView
(mContext, roomId);mLayoutBarrageContainer.addView(barrageStreamView
);
BarrageStreamView
的效果图: |
BarrageStreamView
包含以下几个关键API:public classBarrageStreamView
extends FrameLayout {public void setItemTypeDelegate(BarrageItemTypeDelegate delegate) {...}public void setItemAdapter(int itemType, BarrageItemAdapter adapter) {...}public void insertBarrages(Barrage... barrages) {}}
Item
对应的数据模型是Barrage
,定义如下:// 文件位置:// tuilivekit/src/main/java/com/trtc/uikit/livekit/component/barrage/store/model/Barrage.java// tuilivekit/src/main/java/com/trtc/uikit/livekit/component/barrage/store/model/BarrageUser.javapublic class Barrage {public final BarrageUser user = new BarrageUser(); //发送者public String content; //发送内容public HashMap<String, Object> extInfo = new HashMap<>(); //扩展信息}public class BarrageUser {public String userId;public String userName;public String avatarUrl;public String level;}
BarrageStreamView
由 RecyclerView 实现,它支持多种消息风格样式。BarrageStreamView
通过 setItemTypeDelegate
接口设置代理,由外部决定支持哪些Item
风格样式:public interface BarrageItemTypeDelegate {int getItemType(int position, Barrage barrage);}public void setItemTypeDelegate(BarrageItemTypeDelegate delegate) {...}
BarrageStreamView
通过setItemAdapter
接口,为指定的Item
风格设置对应的adapter
:public interface BarrageItemAdapter {RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent);void onBindViewHolder(RecyclerView.ViewHolder holder, int position, Barrage barrage);}public void setItemAdapter(int itemType, BarrageItemAdapter adapter) {...}
LiveKit
默认的消息风格是0,效果如下:adapter
是 BarrageItemDefaultAdapter
:// 文件路径:tuilivekit/src/main/java/com/trtc/uikit/livekit/component/barrage/view/adapter/BarrageItemDefaultAdapter.javapublic class BarrageItemDefaultAdapter implements BarrageItemAdapter {@NonNull@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent) {View view = mLayoutInflater.inflate(R.layout.livekit_barrage_item_msg, parent, false);return new ViewHolder(view);}void onBindViewHolder(RecyclerView.ViewHolder holder, int position, Barrage barrage) {...}}
BarrageStreamView
通过 insertBarrages
接口插入特殊消息(不同于默认的聊天消息)。BarrageItemTypeDelegate
,增加一种新的消息格式1,默认消息格式仍为0。调用 setItemTypeDelegate
更新代理。public static final int GIFT_VIEW_TYPE_1 = 1;public class BarrageViewTypeDelegate implements BarrageItemTypeDelegate {@Overridepublic int getItemViewType(int position, Barrage barrage) {if (barrage.extInfo != null && barrage.extInfo.containsKey(GIFT_VIEW_TYPE)) {String viewTypeString = String.valueOf(barrage.extInfo.get(GIFT_VIEW_TYPE));if (String.valueOf(GIFT_VIEW_TYPE_1).equals(viewTypeString)) {return GIFT_VIEW_TYPE_1;}}return 0;}}mBarrageStreamView
.setItemTypeDelegate(new BarrageViewTypeDelegate());
BarrageItemAdapter
。调用 setItemAdapter
设置 adapter
:public class GiftBarrageAdapter implements BarrageItemAdapter {@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {LinearLayout ll = new LinearLayout(mContext);ll.addView(new TextView(mContext));return new GiftViewHolder(ll, mDefaultGiftIcon);}@Overridepublic void onBindViewHolder(RecyclerView.ViewHolder holder, int position, Barrage barrage) {GiftViewHolder viewHolder = (GiftViewHolder) holder;viewHolder.bind(barrage);}// GiftViewHolder...}mBarrageStreamView
.setItemAdapter(GIFT_VIEW_TYPE_1, new GiftBarrageAdapter(mContext));
// 在弹幕区插入一条礼物消息Barrage barrage = new Barrage();barrage.content = "gift";barrage.user.userId = sender.userId;barrage.user.userName = sender.userName;barrage.user.avatarUrl = sender.avatarUrl;barrage.user.level = sender.level;barrage.extInfo.put(Constants.GIFT_VIEW_TYPE, GIFT_VIEW_TYPE_1);barrage.extInfo.put(GIFT_NAME, barrage.giftName);barrage.extInfo.put(GIFT_COUNT, giftCount);barrage.extInfo.put(GIFT_ICON_URL, barrage.imageUrl);barrage.extInfo.put(GIFT_RECEIVER_USERNAME, receiver.userName);mBarrageStreamView
.insertBarrages(barrage);
Barrage
的 extInfo
是一个Map
,用于存放自定义数据。setItemTypeDelegate
和 setItemAdapter
时,请注意尽量保留原有的消息格式0和1,以及他们对应的adapter
(BarrageItemDefaultAdapter
和GiftBarrageAdapter
)。BarrageItemAdapter
,比如叫 MyBarrageItemAdapter
,新风格比较简单,只包含一个文本框,字体大小为20,字体颜色为红色,示例代码如下:public class MyBarrageItemAdapter implements BarrageItemAdapter {private final Context mContext;public MyBarrageItemAdapter(Context context) {mContext = context;}@NonNull@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent) {TextView textView = new TextView(mContext);textView.setTextColor(Color.RED);textView.setTextSize(20);return new ViewHolder(textView);}@SuppressLint("SetTextI18n")@Overridepublic void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, Barrage barrage) {final ViewHolder viewHolder = (ViewHolder) holder;viewHolder.textView.setText(barrage.content);}public class ViewHolder extends RecyclerView.ViewHolder {private TextView textView;public ViewHolder(View itemView) {super(itemView);initView(itemView);}private void initView(View itemView) {textView = (TextView) itemView;}}}
MyBarrageItemAdapter
覆盖原有的 BarrageItemDefaultAdapter
:mBarrageDisplayView.setItemAdapter(0, new MyBarrageItemAdapter(mContext));
默认样式 BarrageItemDefaultAdapter | 覆盖后的自定义样式 MyBarrageItemAdapter |
| |
public static final int GIFT_VIEW_TYPE_2 = 2;public class BarrageViewTypeDelegate implements BarrageItemTypeDelegate {@Overridepublic int getItemViewType(int position, Barrage barrage) {if (barrage.extInfo != null && barrage.extInfo.containsKey(GIFT_VIEW_TYPE)) {String viewTypeString = String.valueOf(barrage.extInfo.get(GIFT_VIEW_TYPE));if (String.valueOf(GIFT_VIEW_TYPE_1).equals(viewTypeString)) {return GIFT_VIEW_TYPE_1;} else if (String.valueOf(GIFT_VIEW_TYPE_2).equals(viewTypeString)) {return GIFT_VIEW_TYPE_2;}}return 0;}}mBarrageStreamView
.setItemTypeDelegate(new BarrageViewTypeDelegate());
BarrageItemStyle2Adapter
示例代码如下:public class BarrageItemStyle2Adapter implements BarrageItemAdapter {private final Context mContext;public BarrageItemStyle2Adapter(Context context) {mContext = context;}@NonNull@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent) {TextView textView = new TextView(mContext);textView.setTextColor(Color.YELLOW);textView.setTextSize(12);textView.setBackgroundColor(Color.GRAY);return new ViewHolder(textView);}@SuppressLint("SetTextI18n")@Overridepublic void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, Barrage barrage) {final ViewHolder viewHolder = (ViewHolder) holder;viewHolder.textView.setText(barrage.content);}public class ViewHolder extends RecyclerView.ViewHolder {private TextView textView;public ViewHolder(View itemView) {super(itemView);initView(itemView);}private void initView(View itemView) {textView = (TextView) itemView;}}}
BarrageItemStyle2Adapter
:mBarrageStreamView
.setItemAdapter(GIFT_VIEW_TYPE_2, new BarrageItemStyle2Adapter(mContext));
Barrage barrage = new Barrage();barrage.content = "插入了一条自定义消息,风格类型为2";barrage.extInfo.put(Constants.GIFT_VIEW_TYPE, GIFT_VIEW_TYPE_2);mBarrageStreamView
.insertBarrages(barrage);
本页内容是否解决了您的问题?