onLoadMore,可对接 LiveListStore 或您自己的业务服务端追加下一页数据。直播间切换 | 直播观看 |
![]() |
![]() |
LiveListView 是一个竖向分页的直播间信息流容器,内部基于 ViewPager2 + FragmentStateAdapter 实现;每一页内部都由 PlayerView 承载,因此单个直播间的播放能力(拉流、进房、连麦、弹幕、礼物、点赞)与 观众观看 文档完全一致。import android.os.Bundleimport android.util.Logimport androidx.appcompat.app.AppCompatActivityimport com.trtc.uikit.livekit.livelist.LiveListViewimport io.trtc.tuikit.atomicxcore.api.CompletionHandlerimport io.trtc.tuikit.atomicxcore.api.live.LiveListStoreclass AudienceLiveListActivity : AppCompatActivity() {companion object {private const val TAG = "AudienceLiveListActivity"private const val FETCH_COUNT = 6const val EXTRA_LIVE_ID = "liveID"}private lateinit var liveListView: LiveListViewprivate var isLoading = falseoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val liveID = intent.getStringExtra(EXTRA_LIVE_ID).orEmpty()// 1. 创建 LiveListView,构造时传入的 liveID 会作为首屏直播间// 重写 onLoadMore,在观众滑动到列表末尾前加载更多数据liveListView = object : LiveListView(this, liveID = liveID) {override fun onLoadMore() {loadMore()}}// 2. 将 LiveListView 添加到界面setContentView(liveListView)// 3. 拉取首屏直播间列表并设置给 LiveListViewrefresh()}private fun refresh() {if (isLoading) returnfetch(isRefresh = true, callback = object : CompletionHandler {override fun onSuccess() {liveListView.setData(LiveListStore.shared().liveState.liveList.value)isLoading = false}override fun onFailure(code: Int, desc: String) {Log.e(TAG, "refresh failed, code:$code, desc:$desc")isLoading = false}})}private fun loadMore() {if (isLoading) returnval cursor = LiveListStore.shared().liveState.liveListCursor.valueif (cursor.isEmpty()) returnval oldSize = LiveListStore.shared().liveState.liveList.value.sizefetch(isRefresh = false, callback = object : CompletionHandler {override fun onSuccess() {val newList = LiveListStore.shared().liveState.liveList.valueif (newList.size > oldSize) {liveListView.appendData(newList.subList(oldSize, newList.size))}isLoading = false}override fun onFailure(code: Int, desc: String) {Log.e(TAG, "loadMore failed, code:$code, desc:$desc")isLoading = false}})}private fun fetch(isRefresh: Boolean, callback: CompletionHandler) {isLoading = trueval cursor = LiveListStore.shared().liveState.liveListCursor.valueval fetchCursor = if (isRefresh || cursor.isEmpty()) "" else cursorLiveListStore.shared().fetchLiveList(fetchCursor, FETCH_COUNT, object : CompletionHandler {override fun onSuccess() {callback.onSuccess()}override fun onFailure(code: Int, desc: String) {callback.onFailure(code, desc)}})}}
setData / appendData 注入即可。stopPlay / leaveChannel。// 跳转到「滑动切换直播间」观看页fun enterLiveListPage(context: Context, liveID: String) {val intent = Intent(context, AudienceLiveListActivity::class.java)intent.putExtra(AudienceLiveListActivity.EXTRA_LIVE_ID, liveID)context.startActivity(intent)}
LiveListView 继承自 FrameLayout,是一个竖向分页的直播间信息流容器。方法 / 属性 | 说明 |
LiveListView(context, liveID) | 构造方法。liveID 会作为首屏直播间(置顶到列表第一位);不传则展示列表第一条。 |
setData(liveList: List<LiveInfo>) | 设置直播间列表数据。内部会自动按 liveID 去重,并将构造传入的 liveID 置顶。 |
appendData(liveList: List<LiveInfo>) | 在列表尾部追加直播间数据,用于分页加载。已存在的直播间会被更新而非重复插入。 |
onLoadMore() | 可重写的方法,观众滑动到距离列表末尾还剩 2 个直播间时回调,用于加载下一页数据。 |
AtomicXCore 的 LiveListStore:能力 | 接口 |
拉取列表 | LiveListStore.shared().fetchLiveList(cursor, count, CompletionHandler) |
列表数据 | LiveListStore.shared().liveState.liveList.value |
分页游标 | LiveListStore.shared().liveState.liveListCursor.value(为空表示没有更多数据) |
ViewPager2 + FragmentStateAdapter,需要 FragmentActivity 环境。请将承载 LiveListView 的 Activity 改为继承 AppCompatActivity(或 FragmentActivity)。setData() 注入数据,且列表中的 liveID 不为空;fetchLiveList 回调为 onSuccess;文档反馈