如何在滚动视图中获取滚动到顶部和底部的确切事件

How to get exact events of scrolled to top and bottom in scrollview

我尝试了很多在此处解释的方法 Whosebug.But 我仍然无法完美地获取滚动视图中的每个事件。

我打算做的是在到达顶部或底部时更改 edittext 中的文本(包装在滚动视图中),以便可以加载大文件。

        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
            View view = (View) scrollView.getChildAt(scrollView.getChildCount() - 1);
            int diff = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()));

            // if diff is zero, then the bottom has been reached
            if (diff == 0) {
                if(index<texts.size()-1){
                    scrollView.scrollTo(0,0);
                    index++;
                    mInput.setText(texts.get(index));
                }
            }
                // do stuff similarly for top position reached
        }

尝试这样的事情:

 @Override
 protected void onScrollChanged(int l, int t, int oldl, int oldt) { 

 if(view.getTop()==t){
    // reaches the top end
    }

 View view = (View) getChildAt(getChildCount()-1); 
 int diff = (view.getBottom()-(getHeight()+getScrollY()+view.getTop()));// Calculate the scrolldiff 
 if( diff <= 0 ){ 
    // if diff is zero, then the bottom has been reached
     Log.d(ScrollTest.LOG_TAG, "MyScrollView: Bottom has been reached" );
         }
super.onScrollChanged(l, t, oldl, oldt); 
    }

希望对您有所帮助。