Android:如何检测NestedScrollView何时滚动到底部

发布于 2025-01-21 03:09:13 字数 497 浏览 2 评论 0原文

我尝试了一些方法,并且由于我们的屏幕尺寸不同,所以我无法获得更好的方法。这是我的代码

  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()
        }
    }

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()
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉睡月亮 2025-01-28 03:09:13

这解决了问题

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

This solved the problem

    private fun setupScrollListener() {
    scrollContainer.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { view, _, scrollY, _, _ ->
        if (view.getChildAt(0).bottom <= scrollContainer.height + scrollY) {
           //at bottom
        }else{
          
        }
    })
}
李不 2025-01-28 03:09:13

试试这个

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  
            }          
    }
});

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  
            }          
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文