Like the following, I add multiple elements to the headerList when needed, and refresh, and the headerList will display as many elements as there are.
val pageFlow = Pager(
config = PagingConfig(
pageSize = PAGE_SIZE,
initialLoadSize = INITIAL_LOAD_SIZE,
enablePlaceholders = false,
prefetchDistance = PREFETCH_DISTANCE,
),
initialKey = 1,
pagingSourceFactory = { HiPagingSourceFactory() }
).flow.map { pagingData ->
headerList.fold(pagingData) { acc, item ->
acc.insertHeaderItem(TerminalSeparatorType.FULLY_COMPLETE, item)
}
}.cachedIn(viewModelScope)
val headerList = mutableListOf<T>()
pagingViewModel.headerList.add(VideoCommentInfo(
videoId = videoId,
userName = user?.userName.toString(),
avatar = user?.avatar.toString(),
content = comment,
createdAt = System.currentTimeMillis() / 1000,
resource = if (filePath == null) null else ResourceInfo(0, filePath),
type = VideoCommentType.TYPE_ROOT
))
adapter.refresh()
But I didn’t want to call the refresh method, so I cached the pagingdata object and called the following method.
lifecycleScope.launch {
pagingViewModel.pageFlow.collect {
adapter.submitPagingData(it)
}
}
adapter.insertHeaderItem(
VideoCommentInfo(
videoId = videoId,
userName = user?.userName.toString(),
avatar = user?.avatar.toString(),
content = comment,
createdAt = System.currentTimeMillis() / 1000,
resource = if (filePath == null) null else ResourceInfo(0, filePath),
type = VideoCommentType.TYPE_ROOT
)
)
var pagingData: PagingData<T>? = null
fun insertHeaderItem(item: T) {
val newPagingData = pagingData?.insertHeaderItem(item = item) ?: return
submitPagingData(newPagingData)
}
fun submitPagingData(pagingData: PagingData<T>) {
this.pagingData = pagingData
if (lifecycleOwner != null) {
submitData(lifecycleOwner.lifecycle, pagingData)
return
}
}
But when I call adapter.insertHeaderItem(VideoCommentInfo() ) method multiple times, only the last piece of data can be displayed.