how to detect the position of the scroll nestedscrollview android at the bottom?

后端 未结 7 990
不知归路
不知归路 2020-12-04 22:19

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


        
7条回答
  •  粉色の甜心
    2020-12-04 22:27

    This worked for me :

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

    Basically , we add many views inside a nestedScrollView but wrap them together in a form of Single View. Therefore, childCount will always be 1 with its index 0. Thus, used v.getChildAt(0).getBottom() to determine its bottom. Now, nestedScroll.getHeight() returns height in pixel,and , scrollY will return current vertical origin. Therefore, the above condition will be true everytime the bottom of NestedScrollView is reached.

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

    This only works some time... therefore, don't use it in such way.

提交回复
热议问题