弹幕显示 | 弹幕发送 |
| |
TUIBarrageDisplayView
对外提供 setAdapter
和 TUIBarrageDisplayAdapter
,用于自定义弹幕消息 Item
样式。// 文件位置:tuilivekit/src/main/java/com/trtc/uikit/livekit/common/uicomponent/barrage/view/adapter/TUIBarrageDisplayAdapter.javapublic interface TUIBarrageDisplayAdapter {RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType);void onBindViewHolder(RecyclerView.ViewHolder holder, TUIBarrage barrage);int getItemViewType(int position, TUIBarrage barrage);}
// 文件路径:tuilivekit/src/main/java/com/trtc/uikit/livekit/common/uicomponent/gift/view/GiftBarrageAdapter.javapublic class GiftBarrageAdapter implements TUIBarrageDisplayAdapter {private final Context mContext;public GiftBarrageAdapter(Context context) {mContext = context;}@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {if (viewType == GIFT_VIEW_TYPE_1) {// 自定义样式1的处理LinearLayout ll = new LinearLayout(mContext);ll.addView(new TextView(mContext));return new GiftViewHolder(ll);}return null;}@Overridepublic void onBindViewHolder(RecyclerView.ViewHolder holder, TUIBarrage barrage) {if (holder instanceof GiftViewHolder) {GiftViewHolder viewHolder = (GiftViewHolder) holder;viewHolder.bind(barrage);}}@Overridepublic int getItemViewType(int position, TUIBarrage barrage) {if (...) { // 如果当前 barrage 需要自定义 Item 样式,则返回对应样式typereturn GIFT_VIEW_TYPE_1;}return 0; // 0 表示使用默认样式}private static class GiftViewHolder extends RecyclerView.ViewHolder {public GiftViewHolder(View itemView) {super(itemView);// ...}public void bind(TUIBarrage barrage) {// ...}}}// 设置自定义 AdapterbarrageDisplayView.setAdapter(new GiftBarrageAdapter(mContext));
TUIBarrage
定义如下:// 文件位置:// tuilivekit/src/main/java/com/trtc/uikit/livekit/common/uicomponent/barrage/model/TUIBarrage.java// tuilivekit/src/main/java/com/trtc/uikit/livekit/common/uicomponent/barrage/model/TUIBarrageUser.javapublic class TUIBarrage {public final TUIBarrageUser user = new TUIBarrageUser(); //发送内容public String content; //扩展信息public HashMap<String, Object> extInfo = new HashMap<>(); //扩展信息}public class TUIBarrageUser {public String userId;public String userName;public String avatarUrl;public String level;}
getItemViewType
指定多种返回值),0 表示默认样式。// 文件位置:iOS/TUILiveKit/Source/Common/UIComponent/Barrage/View/Cell/TUIBarrageCell.swiftclass TUIBarrageDefaultCell: UIView {// 普通弹幕消息样式...func constructViewHierarchy() {// 视图层级构建}func activateConstraints() {// 视图layout布局}}
// 文件位置:iOS/TUILiveKit/Source/Common/UIComponent/Gift/View/CustomBarrageCell.swiftclass CustomBarrageCell {static func getCustomCell(barrage: TUIBarrage) -> UIView {// 返回礼物回显消息样式UI}}
TUIBarrage
定义如下:// 文件位置:// iOS/TUILiveKit/Source/Common/UIComponent/Barrage/Model/TUIBarrage.swift// iOS/TUILiveKit/Source/Common/UIComponent/Barrage/Model/TUIBarrageUser.swiftclass TUIBarrage: Codable{var user: TUIBarrageUservar content: Stringvar extInfo: [String: AnyCodable]}class TUIBarrageUser: Codable {var userId: Stringvar userName: Stringvar avatarUrl: Stringvar level: String}
TUIBarrageDisplayView
对外提供 insertBarrages
接口方法,用于(批量)插入自定义消息,通常自定义消息配合自定义样式,实现不一样的展示效果。// 示例:在弹幕区插入一条礼物消息TUIBarrage barrage = new TUIBarrage();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);barrageDisplayView.insertBarrages(barrage);
// 文件位置:// iOS/TUILiveKit/Source/View/LiveRoom/View/Anchor/LivingView.swift// iOS/TUILiveKit/Source/View/LiveRoom/View/Anchor/AudienceLivingView.swift// 示例:在弹幕区插入一条礼物消息let barrage = TUIBarrage()barrage.content = "gift"barrage.user.userId = sender.userIdbarrage.user.userName = sender.userNamebarrage.user.avatarUrl = sender.avatarUrlbarrage.user.level = sender.levelbarrage.extInfo["TYPE"] = AnyCodable("GIFTMESSAGE")barrage.extInfo["gift_name"] = AnyCodable(gift.giftName)barrage.extInfo["gift_count"] = AnyCodable(giftCount)barrage.extInfo["gift_icon_url"] = AnyCodable(gift.imageUrl)barrage.extInfo["gift_receiver_username"] = AnyCodable(receiver.userName)barrageDisplayView.insertBarrages([barrage])
TUIBarrage
的 extInfo
是一个Map
,用于存放自定义数据。TUIBarrageButton
:点击后可以拉起输入界面。TUIBarrageDisplayView
:用于展示弹幕消息。TUIBarrageButton
,点击后可以拉起输入界面:// 文件位置:tuilivekit/src/main/java/com/trtc/uikit/livekit/common/uicomponent/barrage/TUIBarrageButton.javaTUIBarrageButton barrageButton = new TUIBarrageButton(mContext, roomId);mBarrageButtonContainer.addView(barrageButton);
let barrageButton = TUIBarrageButton(roomId: roomId, ownerId: ownerId)addSubView(barrageButton)
TUIBarrageDisplayView
来展示弹幕消息:// 文件位置:tuilivekit/src/main/java/com/trtc/uikit/livekit/common/uicomponent/barrage/TUIBarrageDisplayView.javaTUIBarrageDisplayView barrageDisplayView = new TUIBarrageDisplayView(mContext, roomId);mLayoutBarrageContainer.addView(barrageDisplayView);
let barrageDisplayView = TUIBarrageDisplayView(roomId: roomId, ownerId: ownerId)addSubView(barrageDisplayView)
本页内容是否解决了您的问题?