Home > database >  Android: How to detect when NestedScrollView scrolled to the bottom
Android: How to detect when NestedScrollView scrolled to the bottom

Time:04-13

I have tried some approach and since we have different screen size, I am not able to get a better way. Here is my code

  private val scrollContainer
    get() = View.findViewById<NestedScrollView>(R.id.scroll_container)


 scrollContainer.setOnScrollChangeListener { _, _, newScrollY, _, oldScrollY ->
        val length = 1.5
        val deltaScrollY = newScrollY - oldScrollY
        if (deltaScrollY > length) {
            View.hide()
        }
        if (deltaScrollY < -length) {
            View.show()
        }
    }

CodePudding user response:

This solved the problem

    private fun setupScrollListener() {
    scrollContainer.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { view, _, scrollY, _, _ ->
        if (view.getChildAt(0).bottom <= scrollContainer.height   scrollY) {
           //at bottom
        }else{
          
        }
    })
}

CodePudding user response:

Try this

scroll.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
    @Override
    public void onScrollChanged() {
            View view = (View) scroll.getChildAt(scroll.getChildCount() - 1);
            int diff = (view.getBottom() - (scroll.getHeight()   scroll
                    .getScrollY()));

            if (diff == 0) {
              //your code  
            }          
    }
});
  • Related