question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

3reactions
lukasz-kalnik-gcxcommented, Jan 18, 2021

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

0reactions
Zhuindencommented, Jan 15, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

notifyDataSetChanged example - android - Stack Overflow
For an ArrayAdapter , notifyDataSetChanged only works if you use the add() , insert() , remove() , and clear() on the Adapter.
Read more >
notifyDataSetChanged example - Medium
I know this is a late response but I was facing a similar issue and I managed to solve it by using notifyDataSetChanged()...
Read more >
Calling NotifyAll to Update RecyclerView from External Method
I have a RecyclerView (with an Adapter, ViewHolder, etc.), and I need to update the data (the IEnumerable passed to the Adapter).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found