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
Scroll to first added element #33
Comments
Hi, In general, the item animations are controlled by the recycler view and the diff utils mechanisms, not the adapter. |
Probably video should help to understand what I wrote about) New element, which add to top of current elements in adapter, doesn't appear in top automatically. It's required from user to scroll up to see newly added element - it isn't user friendly, I think. |
Thanks for the video. This is happening because LayoutManager thinks the item is inserted above the first item. This behavior makes sense when you are not at the top of the list but I agree that it is unintuitive when you are at the top of the list. It's not the adapter's responsibility to change this behavior. A possible solution can be found here |
Thank you for explanation, I already found this SO answer, but unable to get acceptable result (works in 1 case of 10, probably).
but layoutManager.scrollToPosition(0) does nothing in 90% of trying and manual scrolling needed. In 10% of case recyclerView correctly scroll to new element, but this is sad statistics. Probably postDelayed handler can help, but this is a reinventing the wheel...
|
I understand your issue, its indeed due to the async nature of diffUtil. I can expose a method on the adapter that will let you register a listener to be notified when the data is changing. Like this:
I've tested and it worked 100% of the time. |
Great solution! Right now it can be fixed with this code:
Works as expected, thank you! Upd. I made some modification to handle update, when element moves to first position.
|
v2.0.2 is out with your request |
Hello, I use MVVM pattern with OneAdapter in my app. When new element added to 0 position in items list, we need to manually scroll up to see recently added element. If, for example, new element has index 1, then all work correct and there is animation of adding element.
Do you know any way to automatically scroll to top of list when new element has index 0 and old element with index 0 was visible by user before updating?
Or, probably, scroll to top of list always - it also acceptable for me)
Short code fragment to update adapter:
Update: When elements in the list takes smaller space then available on screen, animation of adding element with 0 index works correct, but after empty space on screen left, list just stop updating even if the user is at the very beginning of the list and expects to see new items from the top.
The text was updated successfully, but these errors were encountered: