Comments (7)
Hi,
I'm not sure I understand the issue.
Please specify each issue steps in details on how to recreate, expected outcome, and the actual outcome.
Attach code snippets or gifs that represent those issues.
In general, the item animations are controlled by the recycler view and the diff utils mechanisms, not the adapter.
If an item is not visible to the user the animation will not run, else it should. (again based on the diffing mechanism, not the adapter)
from oneadapter.
Probably video should help to understand what I wrote about)
https://dropmefiles.com/G3uhp
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.
from oneadapter.
Thanks for the video.
It's a known issue with RecyclerView and LayoutManager, just google "recyclerview notifyiteminserted not animating" and you will find dozens of questions regarding the same issue.
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
from oneadapter.
Thank you for explanation, I already found this SO answer, but unable to get acceptable result (works in 1 case of 10, probably).
If I call restoreScrollPositionAfterAdAdded right after setItems, diffUtil can't calculate new list properly at this time(?) and in method I get:
firstVisibleItemPosition: 0
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...
Another idea: some callback from oneAdapter should help call restoreScrollPosition at a right time, what do you think?
Current code:
dialogViewModel.allDialogs.observe(this) { dialogs ->
oneAdapter.setItems(dialogs)
restoreScrollPositionAfterAdAdded()
}
from oneadapter.
I understand your issue, its indeed due to the async nature of diffUtil.
You can attach a hook to the RecyclerView to be notified when the data actually changes after the diffing is done.
I can expose a method on the adapter that will let you register a listener to be notified when the data is changing.
The method is registerAdapterDataObserver with a AdapterDataObserver instance passed as a parameter.
With it you can listen to onItemRangeInserted and if the positionStart is 0 scroll to the position.
Like this:
oneAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()
if (firstVisibleItemPosition == 0 && positionStart == 0) {
layoutManager.scrollToPosition(0)
}
}
})
I've tested and it worked 100% of the time.
from oneadapter.
Great solution!
Right now it can be fixed with this code:
binding.dialogsRV.adapter?.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
val layoutManager = (binding.dialogsRV.layoutManager as LinearLayoutManager?) ?: return
val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()
if (firstVisibleItemPosition == 0 && positionStart == 0) {
layoutManager.scrollToPosition(0)
}
}
})
Works as expected, thank you!
I will switch to registerAdapterDataObserver method of the oneAdapter directly, when it will available)
Upd. I made some modification to handle update, when element moves to first position.
Useful for those who have the same question.
val layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
binding.dialogsRV.layoutManager = layoutManager
binding.dialogsRV.adapter?.registerAdapterDataObserver(object :
RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()
if (firstVisibleItemPosition == 0 && positionStart == 0) {
layoutManager.scrollToPosition(0)
}
}
override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()
if (firstVisibleItemPosition == 0 && toPosition == 0) {
layoutManager.scrollToPosition(0)
}
super.onItemRangeMoved(fromPosition, toPosition, itemCount)
}
})
from oneadapter.
v2.0.2 is out with your request
from oneadapter.
Related Issues (20)
- Support for StateRestorationPolicy HOT 4
- Random crash in onCreate HOT 3
- use setItem, but the onBind method not be invoke HOT 12
- Question:Paging with Room Datasource HOT 2
- IndexOutOfBoundsException: Inconsistency detected HOT 2
- CalledFromWrongThreadException in 2.1.0 HOT 9
- Add drag & drop support
- Multiple ViewType HOT 9
- Can we call next page with Paging Module to load without scrolling HOT 5
- Preselected Items HOT 1
- Load more animation when submit empty list HOT 1
- It not a function to get the data from adapter. HOT 1
- Retry button HOT 2
- View Binding with Kotlin 1.6.0 not working HOT 5
- How do i filter the list ?
- You can't use interface as M : Diffable in intem module HOT 1
- select all
- Dynamic layout manager
- Multiple Selection Cancellation Issue When Item Is Not Visible
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from oneadapter.