Groupie: Allow a variant of `update` that is akin to clear+addAll, but using notifyDataSetChanged()

Created on 21 Oct 2020  路  7Comments  路  Source: lisawray/groupie

Is your feature request related to a problem? Please describe
A clear and concise description of what the problem is.

To replace a list of items with a new list of items without using update (DiffUtil), I'm trying to use clear() then addAll(), however, this kills the scroll position on update.

Describe the solution you'd like
A clear and concise description of what you want to happen.

Kinda like the (unfortunately private) setNewGroups method of GroupAdapter, I'd love to do:

adapter.replaceItems(Collection<? extends Group>)

But using adapter.notifyDataSetChanged so that scroll position is preserved.

Additional context
Add any other context about the problem here.

Currently I'm using a workaround

fun GroupieItemAdapter.replaceItemsWith(
    recyclerView: RecyclerView,
    withAnimations: Boolean = false,
    block: MutableList<GroupieItem>.() -> Unit
): GroupieItemAdapter {
    if (withAnimations) {
        this.update(mutableListOf<GroupieItem>().apply(block))
    } else {
        val layoutManager = recyclerView.layoutManager

        val state = layoutManager?.onSaveInstanceState()

        this.clear()
        this.addAll(mutableListOf<GroupieItem>().apply(block))

        layoutManager?.onRestoreInstanceState(state) // fixme
    }
    return this
}

But I'm not really a fan of it, as there's a good chance this is unpredictable with non-LinearLayoutManagers.

enhancement

Most helpful comment

Confirm, in 2.9.0 this feature is available. Thanks for releasing!

All 7 comments

I'd welcome such refresh functionality too. It'd be handy for re-ordering lists.

Note to self that it should also be considered for sections (https://github.com/lisawray/groupie/issues/369) and not just adapter level.

There is a list reordering sample in example and it requires update() to work, otherwise adapter.notifyDataSetChanged() also invalidates the item being dragged, and therefore just "stops" the drag process.

However, this is added, and will be released in the next release (whenever that happens).

Hi @Zhuinden , thanks for implementing this. Any chance when it can be released? I'm also in urgent need of this functionality. Thanks!

I'll try to figure out your other issue first, if that happens then I'll figure out how to make a new release and provide it over Bintray, as replaceAll is pretty important.

I was considering creating a recyclerview-selection integration but I hear that releasing new artifacts is tricky, so that is postponed for now, and the core will not depend on that lib for sure

If I did every step correctly, 2.9.0 is released with this feature.

The bug fix to the other reported issue should go in 2.9.1.

Confirm, in 2.9.0 this feature is available. Thanks for releasing!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nimrodda picture Nimrodda  路  3Comments

mattinger picture mattinger  路  3Comments

jordond picture jordond  路  6Comments

forntoh picture forntoh  路  3Comments

kawaiiDango picture kawaiiDango  路  5Comments