Polyv Help Center

Help Center

6_6-互动学堂场景-聊天室

Updated: 2023-04-17 15:27:57

1 Feature Overview

The chat room module includes features such as posting messages, replying, and viewing history. Compared to other modules, the chat room module is more complex and extensive in terms of UI, interaction, and functionality. Therefore, designing, building, and maintaining a chat room can be time-consuming and labor-intensive. We recommend directly using the chat room module encapsulated by Polyv. This part of the code is fully open-source, supporting direct use and secondary development.

2 Usage Demo

In the interactive classroom, the chat room is a popup layout. Call the constructor of the chat room layout, set up callback listeners, and invoke its initialization method to complete initialization. When you need to open the chat room, call its show() method to display it.

// 聊天室布局构造、设置回调监听
private void initChatroomLayout() {
    chatroomLayout = new PLVHCChatroomLayout(getContext());
    chatroomLayout.setOnViewActionListener(new IPLVHCChatroomLayout.OnViewActionListener() {
        // 聊天室布局的回调监听 ...
    });
}

// 初始化数据
chatroomLayout.init(liveRoomDataManager);

// 需要显示的时候,调用show
chatroomLayout.show(getWidth(), smallScreenHeight, smallScreenLocation);

For detailed calling code and communication with other modules, please refer to the code calls in the PLVHCToolBarLayout class.

3 Interface Introduction

The interface of the interactive classroom chat room layout is defined as IPLVHCChatroomLayout. The interface defines functional methods that can be directly called externally, as well as event listeners that require external responses.

  1. Directly callable functional methods provided externally
/**
 * 初始化
 *
 * @param liveRoomDataManager 直播间数据管理器
 */
void init(IPLVLiveRoomDataManager liveRoomDataManager);

/**
 * 设置view交互事件监听器
 *
 * @param listener 监听器
 */
void setOnViewActionListener(OnViewActionListener listener);

/**
 * 获取聊天室presenter
 *
 * @return 聊天室presenter
 */
IPLVChatroomContract.IChatroomPresenter getChatroomPresenter();

/**
 * 课节准备中
 */
void onLessonPreparing(long serverTime, long lessonStartTime);

/**
 * 课节开始
 */
void onLessonStarted();

/**
 * 课节结束
 */
void onLessonEnd(long inClassTime);

/**
 * 处理图片选择结果
 *
 * @param data 数据
 */
void handleImgSelectResult(Intent data);

/**
 * 显示布局
 */
void show(int viewWidth, int viewHeight, int[] viewLocation);

/**
 * 隐藏
 */
void hide();

/**
 * 是否显示状态
 *
 * @return true:显示,false:不显示
 */
boolean isShown();

/**
 * 是否拦截返回事件
 *
 * @return true:拦截,false:不拦截
 */
boolean onBackPressed();

/**
 * 销毁,释放资源
 */
void destroy();
  1. Event listeners requiring external responses
/**
 * view交互事件监听器
 */
interface OnViewActionListener {
    /**
     * 可见性改变回调
     *
     * @param isVisible true:显示,false:隐藏
     */
    void onVisibilityChanged(boolean isVisible);

    /**
     * 未读信息数改变
     *
     * @param currentUnreadCount 未读信息数
     */
    void onUnreadMsgCountChanged(int currentUnreadCount);
}

4 Implementation Introduction

The implementation class of IPLVHCChatroomLayout is PLVHCChatroomLayout, which includes sub-layouts such as the chat message list, message input box, emoji layout, and like layout.

4.1 Initializing the View

View initialization is performed when the chat room layout is constructed, including creating chat room sub-views, setting callback listeners, initializing the message list, and initializing the emoji list.

private void initView() {
    LayoutInflater.from(getContext()).inflate(R.layout.plvhc_chatroom_layout, this);

    // findViewById ...

    // setOnClickListener ...

    plvhcChatroomCallInputEt.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                v.callOnClick();
            }
            return true;
        }
    });
    plvhcChatroomChatMsgRv.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                closeEmojiSmallLayout();
            }
            return false;
        }
    });

    // 初始化聊天室列表
    PLVMessageRecyclerView.setLayoutManager(plvhcChatroomChatMsgRv).setStackFromEnd(false);
    plvhcChatroomChatMsgRv.addItemDecoration(new PLVMessageRecyclerView.SpacesItemDecoration(ConvertUtils.dp2px(12), 0));
    chatMessageAdapter = new PLVHCMessageAdapter();
    chatMessageAdapter.setOnViewActionListener(new PLVHCMessageAdapter.OnViewActionListener() {
        // 聊天消息列表回调监听 ...
    });
    plvhcChatroomChatMsgRv.setAdapter(chatMessageAdapter);
    plvhcChatroomChatMsgRv.addUnreadView(plvhcChatroomMoreMsgTv);
    plvhcChatroomChatMsgRv.addOnUnreadCountChangeListener(new PLVMessageRecyclerView.OnUnreadCountChangeListener() {
        @Override
        public void onChange(int currentUnreadCount) {
            plvhcChatroomMoreMsgTv.setText(currentUnreadCount + "条新信息");
        }
    });

    // 初始化下拉加载历史记录控件
    plvhcChatroomSwipeLoadView.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light,
            android.R.color.holo_orange_light, android.R.color.holo_green_light);
    plvhcChatroomSwipeLoadView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            chatroomPresenter.requestChatHistory(chatroomPresenter.getViewIndex(chatroomView));
        }
    });

    // 初始化表情列表
    PLVHCChatroomUtils.initEmojiList(plvhcChatroomEmojiRv, plvhcChatroomCallInputEt);

    // 默认不可用状态
    disableChatroom();
}

4.2 Initializing Data

Data initialization includes receiving and saving the data manager IPLVLiveRoomDataManager, registering and initializing the Presenter IChatroomPresenter, requesting historical chat data, and logging into the chat room socket.

@Override
public void init(IPLVLiveRoomDataManager liveRoomDataManager) {
    this.liveRoomDataManager = liveRoomDataManager;
    String userType = liveRoomDataManager.getConfig().getUser().getViewerType();
    isTeacherType = PLVSocketUserConstant.USERTYPE_TEACHER.equals(userType);
    plvhcChatroomCloseRoomIv.setVisibility(isTeacherType ? View.VISIBLE : View.GONE);

    this.chatroomPresenter = new PLVChatroomPresenter(liveRoomDataManager);
    this.chatroomPresenter.registerView(chatroomView);
    this.chatroomPresenter.init();
    //请求一次历史记录
    chatroomPresenter.requestChatHistory(chatroomPresenter.getViewIndex(chatroomView));

    initSocketLoginManager();
}

The chat room is implemented using the MVP pattern, with specific interaction logic implemented in chatroomView. For details, refer to the code block Chat Room - MVP Pattern View Layer Implementation.

联系客服,在线咨询