Open lisawray opened 6 years ago
I'd be happy to assist with some snippets for the LiveData-variant (it's really simple):
Add to your gradle-file:
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
Create ViewModel:
import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class ExampleViewModel : ViewModel() {
fun exampleData(context: Context): LiveData<List<Int>> {
return MutableLiveData<List<Int>>().apply {
// Get your data and set it as `value` each time it changes.
// We use a static list as example.
value = context.resources.getIntArray(R.array.rainbow_200).asList()
}
}
}
Add this to the Activity
:
private val viewModel = ViewModelProviders.of(this).get(ExampleViewModel::class.java)
override fun onCreate(savedInstanceState: Bundle?) {
viewModel.exampleData(this).observe(this, Observer {
groupAdapter.updateAsync(it.map { CardItem(it) })
})
}
Hints:
Use viewLifecycleOwner
(via .observe(viewLifecycleOwner, Observer { })
instead of this
when using a Fragment
.
Update value
of your LiveData
(e.g. via a listener on your data) each time it changes. This will call the Observer
and thereby updateAsync()
.
Is there any update on this? would love some idea of how to get updateAsync() working while still using expandable groups.
How would you call updateAsync and add the items to a specific section?
How would you call updateAsync and add the items to a specific section?
Yep, how this can be done?
I'd like to show how all the original features of Groupie people know and love -- expanding groups, updates to individual sections and item payload updates, etc ... can be accomplished using the Architecture Components ViewModels and updateAsync().
Unless I get a volunteer to write it with LiveData, I'll do it with Rx because that's all I know :)