Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Zhuinden opened this issue Oct 21, 2020 · 7 comments

Comments

@Zhuinden
Copy link
Collaborator

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.

@Zhuinden Zhuinden added enhancement waiting for owners Waiting on an answer by project owners labels Oct 21, 2020
@Zhuinden Zhuinden added triaged Seen it and is expected to be done eventually. and removed waiting for owners Waiting on an answer by project owners labels Nov 13, 2020
@vkay94
Copy link

vkay94 commented Nov 16, 2020

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

@Zhuinden
Copy link
Collaborator Author

Note to self that it should also be considered for sections (#369) and not just adapter level.

@Zhuinden
Copy link
Collaborator Author

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

@lukasz-kalnik-gcx
Copy link

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

@Zhuinden
Copy link
Collaborator Author

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

@Zhuinden
Copy link
Collaborator Author

Zhuinden commented 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.

@lukasz-kalnik-gcx
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants