How can I add new Items to a MutableLiveData List? I want to build an infinite scrolling Recyclerview. So I have a List with 10 items and after clicking a "load more button" I want to add ten new Items.
This are my lists of products:
private companion object {
private var _products = MutableLiveData<MutableList<Product>>()
}
val products: LiveData<MutableList<Product>> = _products
Ant this is my loading function:
fun loadProducts(category: Category) { // category: String
_isViewLoading.postValue(true)
AppService.fetchProducts(category, neuheitenCounter) { success, response ->
_isViewLoading.postValue(false)
if(success) {
when(category) {
Category.NEWS -> {
if(_products.value == null) {
_products.value = response?.products //as List<Product>
} else {
response?.let {
_products.value?.addAll(response?.products)
}
}
neuheitenCounter++
}
}
}
}
}
If I call _products.value = response?.products
it fires the observe method in my Activity. But if I call addAll
or add
, the observer method is not called.