如何检测滚动嵌套scrollview android在底部的位置?

2g32fytz  于 2022-12-16  发布在  Android
关注(0)|答案(9)|浏览(137)

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

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

但是总高度和最大滚动不一样。怎么解决?

avwztpqn

avwztpqn1#

NestedScrollView参数中设置setOnScrollChangeListener以获取

  • 嵌套滚动视图v(带滚动的父视图)
  • 中间滚动Y
  • 整数旧滚动Y

要检测偏移量是否在底部,需要获取内容高度v.getChildAt(0).getMeasuredHeight()的值,并比较当前滚动过父高度,如果有相同的值,则说明已经到了末尾。
可以使用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");
           }
       }
    });
}
lf5gs5x2

lf5gs5x22#

我知道很晚了,但是......试试这边。

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) {
                getPlaylistFromServer("more");
            }          
    }
});

快乐编码..

mkh04yzy

mkh04yzy3#

Webserveis回答正确,但需要在onScrollChange(override方法)中做一点修改,如下所示:

if (scrollY === v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight()) {
   // end of the scroll view
}

Kotlin:

if (scrollY == v.getChildAt(0).measuredHeight - v.measuredHeight) {
    // end of the scroll view
}
0pizxfdo

0pizxfdo4#

@Override
public void onScrollChange(NestedScrollView nestedScrollView, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
    if (nestedScrollView.getChildAt(nestedScrollView.getChildCount() - 1) != null) {
        if ((scrollY >= (nestedScrollView.getChildAt(nestedScrollView.getChildCount() - 1).getMeasuredHeight() - nestedScrollView.getMeasuredHeight())) &&
                scrollY > oldScrollY) {
            LogsUtils.INSTANCE.makeLogD(">onScrollChange>", ">>BOTTOm");
        }

    }
}

它对我起作用了Source

kx7yvsdv

kx7yvsdv5#

这对我很有效:

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

            if (v.getChildAt(0).getBottom()<=(nestedScroll.getHeight()+scrollY)) {
                System.out.println("End of NestedScrollView");
            }

        }
    });

基本上,我们在nestedScrollView中添加许多视图,但以Single View的形式将它们 Package 在一起。因此,childCount将始终为1,其索引为0。因此,使用v.getChildAt(0).getBottom()来确定其底部。现在,nestedScroll.getHeight()返回像素高度,scrollY将返回当前垂直原点。因此,每当到达NestedScrollView的底部时,上述条件将为真。

if (v.getChildAt(0).getBottom()==(nestedScroll.getHeight()+nestedScroll.getScrollY()))

这只工作了一段时间...因此,不要以这种方式使用它。

amrnrhlw

amrnrhlw6#

对于API〈23,您可以添加一个treeObserver。scrollChangeLister存储一个本地浮点变量,并检查您滚动方向,如下所示
范例

public class About extends AppCompatActivity implements 
ViewTreeObserver.OnScrollChangedListener{

private float viewScrolled = 0;

   nestedScrollView.getViewTreeObserver().addOnScrollChangedListener(this);

}

@Override
public void onScrollChanged() {

    if (viewScrolled < nestedScrollView.getScrollY()){
        viewScrolled = nestedScrollView.getScrollY();
        Log.d(TAG, "scrolling up");
    }
    if (viewScrolled > nestedScrollView.getScrollY()){
        viewScrolled = nestedScrollView.getScrollY();
        Log.d(TAG, "scrolling down");
    }
}
m3eecexj

m3eecexj7#

Webserveis回答正确,但条件应该是这样的

if (scrollY == (v?.getChildAt(0)?.measuredHeight ?: 0) - (v?.measuredHeight ?: 0)) {
    //at bottom
    }
hrysbysz

hrysbysz8#

这对我很有效!

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


                    if (scrollY == v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight()) {
                        Log.d(TAG, "onScrollChange: SRCOLLED ==> scrollY: ======================================> THIS IS THE VERY END ");
                    }

                

            }
        });
yr9zkbsy

yr9zkbsy9#

我只是修改了一点第一个答案的代码,以检测嵌套滚动视图中的循环视图的结尾。

binding.nestedScroll.setOnScrollChangeListener(object:NestedScrollView.OnScrollChangeListener {
        override fun onScrollChange(
            v: NestedScrollView,
            scrollX: Int,
            scrollY: Int,
            oldScrollX: Int,
            oldScrollY: Int
        ) {

        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 (v.getChildAt(0).getBottom() <= v.height + scrollY)
            {
            Log.i("TAG", "BOTTOM SCROLL IN Recyclerview.");

            Toast.makeText(requireContext(), "Last", Toast.LENGTH_LONG).show()
        }

        }
    })

相关问题