Automatic scrolling when new data set
See original GitHub issueI have a weird problem with autoscrolled position if I set data to my controller. I have a screen with pagination, i have ProgressModel that is at the bottom of the list and I show/hide it according to loading state. It looks that when I display progress and then show data (not hiding progress), recyclerview is scrolled so the progress is still visible.
My controller is really simple
class ProductsEpoxyController : EpoxyController() {
var products: List<ApiProduct> by adapterProperty(this, listOf())
var showError by adapterProperty(this, false)
var showProgress by adapterProperty(this, true)
override fun buildModels() {
products.forEach {
ProductModel_()
.id(it.id)
.product(it)
.addTo(this)
}
ProgressModel()
.id(-100)
.addIf(showProgress, this)
}
}
adapterProperty is just a shortcut for rebuilding models when property changes.
The flow of commands is
showProgress = true … (few hundred millis) products = listOf(…)
The recyclerview is scrolled at the bototm and progress is visible.
Im not really sure if this problem is related to this library, but I have a feeling that I had this issue with first version of Epoxy and I solved that by replacing Epoxy with own solution.
I can provide any more info if needed. Thanks
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:12 (3 by maintainers)
@davidbilik I can reproduce it in the Epoxy sample, the key is that the items have to be inserted at position 0. If that happens RecyclerView tries to keep the previous items in frame since it assumes that is what the user was looking at. Generally in our UI’s we have a header at position 0 so we don’t have the scrolling problem.
I think this behavior is often good for the user, but definitely not what we want in the case of a loader. I’m not sure the best way to disable this behavior for your use case. The easiest thing to do is have a header item at position 0. Another option is to temporarily remove the loader when you insert the new items. A third option is to register an adapter change listener and force a scroll to the top
Hm, we do something very similar in many places in our app and haven’t had this problem. This is probably just RecyclerView being weird, Epoxy never tells the recyclerview to scroll, it only dispatches item change notifications (in this case that items were inserted).
One reason I have seen RecyclerView auto scroll is if a view is focusable. In that case it scrolls to keep it in position. If that is the case you could make your loader not focusable, or set
android:descendantFocusability="beforeDescendants"
on your RecyclerView.As another sanity check I would recommend turning on logging (https://github.com/airbnb/epoxy/wiki/Epoxy-Controller#debug-logs) and seeing what change notifications your controller is doing.
I believe this might also happen if you have your LayoutManager set to stack from bottom, or some other layout manager setting to keep the bottom in focus.