BarrageInputView
) and Message List View (BarrageStreamView
):BarrageInputView
: Click to pull up the input interfaceBarrageStreamView
: Used to display the barrage message listBarrageInputView
. After clicking, you can pull up the input interface:// File Location: tuilivekit/src/main/java/com/trtc/uikit/livekit/component/barrage/BarrageInputView
.javaBarrageInputView
barrageInputView = newBarrageInputView
(mContext, roomId); mBarrageInput
Container.addView(barrageInputView);
BarrageInputView
. After clicking, the input panel pops up (red box on the right image) |
BarrageStreamView
to display barrage messages:// File Location: tuilivekit/src/main/java/com/trtc/uikit/livekit/component/barrage/BarrageStreamView
.javaBarrageStreamView
barrageStreamView
= newBarrageStreamView
(mContext, roomId); mLayoutBarrageContainer.addView(barrageStreamView
);
BarrageStreamView
: |
BarrageStreamView
includes the following key APIs:public classBarrageStreamView
extends FrameLayout {public void setItemTypeDelegate(BarrageItemTypeDelegate delegate) {...}public void setItemAdapter(int itemType, BarrageItemAdapter adapter) {...}public void insertBarrages(Barrage... barrages) {}}
Item
in the Danmu Message List corresponds to the data model Barrage
, defined as follows:// File Location:// 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(); //Sender public String content; //Sent content public HashMap<String, Object> extInfo = new HashMap<>(); //Expanded information }public class BarrageUser { public String userId; public String userName; public String avatarUrl; public String level; }
BarrageStreamView
for the Danmu message list view is implemented by RecyclerView and supports various message styles.BarrageStreamView
uses the setItemTypeDelegate
interface to set a proxy, externally determining which Item
styles are supported:public interface BarrageItemTypeDelegate { int getItemType(int position, Barrage barrage); }public void setItemTypeDelegate(BarrageItemTypeDelegate delegate) {...}
BarrageStreamView
sets the corresponding adapter
for the specified Item
style through the setItemAdapter
interfacepublic interface BarrageItemAdapter {RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent); void onBindViewHolder(RecyclerView.ViewHolder holder, int position, Barrage barrage); }public void setItemAdapter(int itemType, BarrageItemAdapter adapter) {...}
LiveKit
is 0, and the effect is as follows:adapter
corresponding to the default message style 0 is BarrageItemDefaultAdapter
:// File Location: 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
inserts special messages through the insertBarrages
interface (different from the default chat messages).BarrageItemTypeDelegate
and add a new message format as 1, while the default message format remains 0. Invoke setItemTypeDelegate
to update the agent.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
. Invoke setItemAdapter
to set the 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));
// Example: Insert a gift message in the barrage area.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
's extInfo
is a Map
, used for storing custom data.setItemTypeDelegate
and setItemAdapter
, please note to retain the original message formats 0 and 1, as well as their corresponding adapters
(BarrageItemDefaultAdapter
and GiftBarrageAdapter
).BarrageItemAdapter
, such as called MyBarrageItemAdapter
. The new style is relatively simple, containing only one text box with a font size of 20 and a font color of red. Example code as follows: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
to overwrite the original BarrageItemDefaultAdapter
:mBarrageDisplayView.setItemAdapter(0, new MyBarrageItemAdapter(mContext));
Default style BarrageItemDefaultAdapter | Custom style after covering 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
example code as follows: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;}}}
mBarrageStreamView
.setItemAdapter(GIFT_VIEW_TYPE_2, new BarrageItemStyle2Adapter(mContext));
Barrage barrage = new Barrage();barrage.content = "Insert a custom message with a style of 2";barrage.extInfo.put(Constants.GIFT_VIEW_TYPE, GIFT_VIEW_TYPE_2);mBarrageStreamView
.insertBarrages(barrage);
Was this page helpful?