1

How can I detect if a scroll view is scrolled to the top? I would like to hide a view if the scroll view is not scrolled to the top and show if it is. I am coding this in kotlin.

QThompson
  • 1,599
  • 3
  • 16
  • 40
  • It's sound a bit like a reason to use Coordinator Layout: https://medium.com/martinomburajr/android-design-coordinator-layout-1-an-introduction-10a1b91ded28 – Cililing Oct 17 '19 at 06:43

2 Answers2

3

there are many ways to get scroll position,

scrollView.viewTreeObserver.addOnScrollChangedListener {
    if (scrollView.scrollY <= 0) {
        // scroll top
    } else {
        // other
    }
}


requires API level 23

scrollView.setOnScrollChangeListener { _, _, scrollY, _, _ ->
    if (scrollY <= 0) {
        // scroll top
    } else {
        // other
    }
}

Can I have onScrollListener for a ScrollView?

this link will help you

ji Ba
  • 98
  • 5
0
scrollView.getViewTreeObserver().addOnScrollChangedListener(OnScrollChangedListener {
                val scrollY = rootScrollView.getScrollY() 
                val scrollX = rootScrollView.getScrollX() // Use this coordinate to know where you are

})
Akshay Raiyani
  • 1,243
  • 9
  • 21