Fixed to to reset carousel position when CarouselItem is displayed again#309
Fixed to to reset carousel position when CarouselItem is displayed again#309NUmeroAndDev wants to merge 2 commits intolisawray:masterfrom
Conversation
There was a problem hiding this comment.
I don't think its appropriate to do this in this way. I've played around with the problem and the most reliable way to restore the scroll position is to store it in a variable at the unbind call and then restore it in bind:
class CarouselItem(private val carouselDecoration: RecyclerView.ItemDecoration,
private val carouselAdapter: GroupAdapter<com.xwray.groupie.GroupieViewHolder>) : Item() {
.....
private var visibleItemPos = 0
.....
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
viewHolder.recyclerView.apply {
adapter = carouselAdapter
........
this.scrollToPosition(visibleItemPos)
}
}
override fun unbind(viewHolder: GroupieViewHolder) {
super.unbind(viewHolder)
visibleItemPos = (viewHolder.recyclerView.layoutManager as? LinearLayoutManager)?.findFirstVisibleItemPosition() ?: 0
}
}If you agree with this feel free to apply the change to both classes and I can approve
| return super.createViewHolder(itemView).apply { | ||
| recyclerView.apply { | ||
| layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) | ||
| adapter = carouselAdapter |
There was a problem hiding this comment.
This will cause an issue if you have more than one CarouselItem in the adapter and you rebind the item. It means both will share the adapter of the first one instead of the 2nd one having a new adapter set because createViewHolder will only have been called once.
Hey @ValCanBuild. Position restoration isn't perfect due to missing offset calculations. Is there any reason why the following approach shouldn't be used? Seems to result in a perfect restoration without any noticeable performance issues class CarouselItem(private val carouselDecoration: RecyclerView.ItemDecoration,
private val carouselAdapter: GroupAdapter<com.xwray.groupie.GroupieViewHolder>) : Item() {
.....
private var recyclerViewState : Parcelable? = null
.....
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
viewHolder.recyclerView.apply {
adapter = carouselAdapter
........
(layoutManager as LinearLayoutManager).onRestoreInstanceState(recyclerViewState)
}
}
override fun unbind(viewHolder: GroupieViewHolder) {
super.unbind(viewHolder)
recyclerViewState = (viewHolder.recyclerView.layoutManager as LinearLayoutManager).onSaveInstanceState()
}
} |
|
The code in the discussion here does not seem to match the code on the branch, that's unfortunate because I think the code discussed here is technically correct. 😅 |
Overview
Screenshot