Skip to content

Commit

Permalink
fix scrollTo only works if previously scrolled to the top
Browse files Browse the repository at this point in the history
  • Loading branch information
westnordost committed Sep 1, 2022
1 parent 53d9bc8 commit e517f2e
Showing 1 changed file with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ abstract class AGroupedImageListQuestForm<I, T> : AbstractOsmQuestForm<T>() {
imageSelector.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
super.onItemRangeInserted(positionStart, itemCount)
scrollTo(positionStart)
scrollTo(positionStart - 1)
}
})
checkIsFormComplete()
Expand All @@ -87,16 +87,14 @@ abstract class AGroupedImageListQuestForm<I, T> : AbstractOsmQuestForm<T>() {
}

private fun scrollTo(index: Int) {
val item = imageSelector.gridLayoutManager.getChildAt(max(0, index - 1))
if (item != null) {
val itemPos = IntArray(2)
item.getLocationInWindow(itemPos)
val scrollViewPos = IntArray(2)
scrollView.getLocationInWindow(scrollViewPos)

scrollView.postDelayed(250) {
scrollView.smoothScrollTo(0, itemPos[1] - scrollViewPos[1])
}
val item = imageSelector.gridLayoutManager.findViewByPosition(index) ?: return
val itemPos = IntArray(2)
item.getLocationInWindow(itemPos)
val scrollViewPos = IntArray(2)
scrollView.getLocationInWindow(scrollViewPos)

scrollView.postDelayed(250) {
scrollView.smoothScrollTo(0, itemPos[1] - scrollViewPos[1])
}
}

Expand Down

0 comments on commit e517f2e

Please sign in to comment.