如何检测底部的滚动nestedscrollview android的位置? [英] how to detect the position of the scroll nestedscrollview android at the bottom?

查看:299
本文介绍了如何检测底部的滚动nestedscrollview android的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在底部检测滚动nestedscrollview android的位置,并调用函数. 我的代码是:

i just want to detect the position of the scroll nestedscrollview android at the bottom, and the to call function. my code is :

scroll.getViewTreeObserver()
      .addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
           @Override
           public void onScrollChanged() {
               int totalHeight = scroll.getChildAt(0).getHeight();
               int scrollY = scroll.getScrollY();
               Log.v("position", "totalHeight=" + totalHeight + "scrollY=" + scrollY);
               if (scrollY==totalHeight) {
                   getPlaylistFromServer("more");
               }
           }
      });

但是总高度与MAX ScrollY不同.如何解决呢?

but totalheight not same wit MAX ScrollY. how to fix it ?

推荐答案

NestedScrollView参数中设置setOnScrollChangeListener以获得

  • NestedScrollView v(带滚动的父级)
  • int scrollY
  • int oldScrollY

要检测偏移量是否在底部,有必要获取内容高度v.getChildAt(0).getMeasuredHeight()的值,并比较当前在父级高度上的滚动,如果您具有相同的值,则表示它具有到达终点.

To detect whether the offset is at the bottom, it is necessary to obtain the value of content height v.getChildAt(0).getMeasuredHeight() and compare the current scroll over the height of the parent, if you have the same value , it means that it has reached the end.

您可以通过v.getMeasuredHeight()

NestedScrollView scroller = (NestedScrollView) findViewById(R.id.myScroll);

if (scroller != null) {

    scroller.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
        @Override
        public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

            if (scrollY > oldScrollY) {
                Log.i(TAG, "Scroll DOWN");
            }
            if (scrollY < oldScrollY) {
                Log.i(TAG, "Scroll UP");
            }

            if (scrollY == 0) {
                Log.i(TAG, "TOP SCROLL");
            }

           if (scrollY == ( v.getMeasuredHeight() - v.getChildAt(0).getMeasuredHeight() )) {
               Log.i(TAG, "BOTTOM SCROLL");
           }
       }
    });
}

这篇关于如何检测底部的滚动nestedscrollview android的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆