如何在scrollview中获取滚动到顶部和底部的精确事件

时间:2016-01-09 20:13:52

标签: android scrollview

我在stackoverflow上尝试了很多解释的方法。但是我仍然无法在scrollview中完美地获取每个事件。

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

        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
        }

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

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

希望它有所帮助。