Display Barrage | Send Barrage |
| |
TUIBarrageDisplayView
provides setAdapter
and TUIBarrageDisplayAdapter
for customizing the pop-up message Item
style. If you need the Self Definition message view, please refer to the following path for changes.// File Location: 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);}
// File Location: 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) {// Handling of custom style 1LinearLayout 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 (...) { // If the current barrage requires a custom Item style, return the corresponding style type.return GIFT_VIEW_TYPE_1;}return 0; // 0 indicates that the default style is used}private static class GiftViewHolder extends RecyclerView.ViewHolder {public GiftViewHolder(View itemView) {super(itemView);// ...}public void bind(TUIBarrage barrage) {// ...}}}// set custom AdapterbarrageDisplayView.setAdapter(new GiftBarrageAdapter(mContext));
TUIBarrage
is defined as follows:// File Location:// 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(); //Senderpublic String content; //Sent contentpublic HashMap<String, Object> extInfo = new HashMap<>(); //Expanded informati}public class TUIBarrageUser {public String userId;public String userName;public String avatarUrl;public String level;}
getItemViewType
), 0 represents the default style.// File Location: iOS/TUILiveKit/Source/Common/UIComponent/Barrage/View/Cell/TUIBarrageCell.swiftclass TUIBarrageDefaultCell: UIView {// Ordinary Danmaku Message Style...func constructViewHierarchy() {// View Hierarchy Construction}func activateConstraints() {// View Layout}}
// File Location: iOS/TUILiveKit/Source/Common/UIComponent/Gift/View/CustomBarrageCell.swiftclass CustomBarrageCell {static func getCustomCell(barrage: TUIBarrage) -> UIView {// Returns the UI style for gift echo messages}}
TUIBarrage
Definition is as follows:// File Location:// 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
provides the insertBarrages
interface method for (batch) inserting custom Definition messages. Typically, custom Definition messages combined with custom styles achieve unique display effects.// Example: Insert a gift message in the barrage area.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);
// File Location:// iOS/TUILiveKit/Source/View/LiveRoom/View/Anchor/LivingView.swift// iOS/TUILiveKit/Source/View/LiveRoom/View/Anchor/AudienceLivingView.swift// Example: Inserting a gift message in the barrage arealet 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
's extInfo
is a Map
, used to store custom data.TUIBarrageButton
: Clicking it brings up the input interface.TUIBarrageDisplayView
: Used to display barrage messages.TUIBarrageButton
, click to bring up the input interface://File Location: 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
to display barrage messages:// File Location: 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)
Was this page helpful?