lisawray / groupie

Groupie helps you display and manage complex RecyclerView layouts.
MIT License
3.67k stars 292 forks source link

Demonstrate updateAsync in example project & readme #189

Open lisawray opened 6 years ago

lisawray commented 6 years ago

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 :)

hardysim commented 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:

userdsod commented 5 years ago

Is there any update on this? would love some idea of how to get updateAsync() working while still using expandable groups.

Kalianey commented 4 years ago

How would you call updateAsync and add the items to a specific section?

Veeksi commented 2 years ago

How would you call updateAsync and add the items to a specific section?

Yep, how this can be done?