Adjust library sublist scroll on changes
parent
7c659371a9
commit
e6202103bc
@ -0,0 +1,31 @@
|
|||||||
|
package org.koitharu.kotatsu.library.ui.adapter
|
||||||
|
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
|
||||||
|
class ScrollKeepObserver(
|
||||||
|
private val recyclerView: RecyclerView,
|
||||||
|
) : RecyclerView.AdapterDataObserver() {
|
||||||
|
|
||||||
|
private val layoutManager: LinearLayoutManager
|
||||||
|
get() = recyclerView.layoutManager as LinearLayoutManager
|
||||||
|
|
||||||
|
override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
|
||||||
|
val position = minOf(toPosition, fromPosition) // if items are swapping positions may be swapped too
|
||||||
|
if (position < layoutManager.findFirstVisibleItemPosition()) {
|
||||||
|
postScroll(position)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
|
||||||
|
if (positionStart < layoutManager.findFirstVisibleItemPosition()) {
|
||||||
|
postScroll(positionStart)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun postScroll(targetPosition: Int) {
|
||||||
|
recyclerView.post {
|
||||||
|
layoutManager.scrollToPositionWithOffset(targetPosition, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue