如题,我的 ViewModel 类中有一个列表结构的数据,需要动态加载,代码如下:
class TimelineViewModel : ViewModel() {
val timelines = MutableLiveData<List<TimelineModel>>(emptyList())
private var nextTime = System.currentTimeMillis()
fun getTimeline() {
viewModelScope.launch {
try {
val respData = Repository.getTimeline(nextTime)
if (respData.data != null) {
nextTime = respData.data.feeds.nextTime
val timelineModels = respData.data.feeds.data.filterNotNull().map {
TimelineModel(it)
}
timelines.value = timelines.value.let {
if (it == null) {
timelineModels
} else {
it + timelineModels
}
}
}
} catch (e: Exception) {
Log.e(TAG, "Network Request Fail", e)
}
}
}
}
MutableLiveData#observe
方法观察这个数据,那么观察到的是整个 List 数据的变化,那么该怎么将其转化为 Adapter 的添加元素的行为?