# 4\_3-带货场景-聊天室

* [1 功能概述](#1-功能概述)
* [2 使用演示](#2-使用演示)
* [3 实现介绍](#3-实现介绍)
  * [3.1 回放 - PLVECPalybackHomeFragment](#31-回放---plvecpalybackhomefragment)
  * [3.2 直播 - PLVECLiveHomeFragment](#32-直播---plveclivehomefragment)

#### 1 功能概述

聊天室模块包括发言、点赞、历史记录、欢迎语等功能。聊天室模块在UI、交互、功能上，相较于其他模块，都会更复杂更庞大。因此设计、搭建、维护一个聊天室，也是一件较为费时费力的事情。我们推荐直接使用保利威封装好的聊天室模块，该部分代码完全开源，支持直接使用，以及二次开发。

#### 2 使用演示

聊天室在带货场景分为直播首页`PLVECLiveHomeFragment`的聊天室和回放首页`PLVECPalybackHomeFragment`中的聊天室。

聊天室遵循`MVP`模式，因此方法之间的调用非常简单明了，在`View`中可以自由选择感兴趣的聊天室事件进行监听，并调用`Presenter`相关方法进行聊天室的操作。

以回放聊天室监听公告事件为例，代码如下：

```java
// PLVECPalybackHomeFragment.java
private IPLVChatroomContract.IChatroomView chatroomView = new PLVAbsChatroomView() {
    // 实现了接受公告事件的接口方法，监听公告事件
    @Override
    public void onBulletinEvent(@NonNull PolyvBulletinVO bulletinVO) {
        super.onBulletinEvent(bulletinVO);
        // 在acceptBulletinMessage方法中处理监听接受到的公告事件，显示到界面当中
        acceptBulletinMessage(bulletinVO);
    }

    // 实现了接受移除公告事件的方法，监听移除公告事件
    @Override
    public void onRemoveBulletinEvent() {
        super.onRemoveBulletinEvent();
        // 移除界面中的公告
        removeBulletin();
    }
};
```

#### 3 实现介绍

**3.1 回放 - PLVECPalybackHomeFragment**

带货场景下的回放聊天室，用于显示公告的更新。该类和聊天室mvp-presenter的通信，是通过该类中定义的聊天室mvp-view，当调用聊天室mvp-presneter的registerView方法后，聊天室mvp-presneter即可通知聊天室mvp-view的更新，同时聊天室mvp-view也能调用调用聊天室mvp-presenter的方法。定义聊天室mvp-view的示例代码：

```java
// 聊天室presenter（定义在父类PLVECCommonHomeFragment当中）
protected IPLVChatroomContract.IChatroomPresenter chatroomPresenter;

// PLVECLiveHomeFragment.java
// 聊天室mvp-view
private IPLVChatroomContract.IChatroomView chatroomView = new PLVAbsChatroomView() {
    // 聊天室需要监听事件的抽象方法实现...
}

// 注册mvp-view
@Override
protected void registerChatroomView() {
    chatroomPresenter.registerView(chatroomView);

    //设置信息索引，需在chatroomPresenter.registerView后设置
    chatMessageAdapter.setMsgIndex(chatroomPresenter.getViewIndex(chatroomView));
}
```

**3.2 直播 - PLVECLiveHomeFragment**

带货场景下的直播聊天室，包含聊天信息列表、发送信息输入框、点赞布局、欢迎语、公告等元素。mvp-view和mvp-presenter之间的构建逻辑与回放相似，在直播聊天室中包含了更多直播情景所需监听的聊天室事件用例，详情请参考demo中`PLVECLiveHomeFragment`的*聊天室*相关代码块。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://polyv.gitbook.io/document/docs/live/android/43-dai-huo-chang-jing-liao-tian-shi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
