Android scrollview onScrollChanged

前端 未结 11 1388
不思量自难忘°
不思量自难忘° 2020-12-08 20:36

I have a fixed content in my text view inside a scroll view. When the user scrolls to a certain position, I would like to start an activity or trigger a Toast.<

相关标签:
11条回答
  • 2020-12-08 21:01

    This question is fairly old, but in case somebody drops by (like me):

    Starting with API 23, Android's View has a OnScrollChangeListener and the matching setter.

    The NestedScrollView from the Support library also supports setting a scroll listener even before that API level. As far as I know, NestedScrollView can be used as a replacement for the normal ScrollView without any problems.

    0 讨论(0)
  • 2020-12-08 21:01

    I extended my scrollView. This link may help.

    class MyScroll extends ScrollView {
        boolean onTop=true;
        @Override
        protected void onScrollChanged(int l, int t, int oldl, int oldt) {
            //Log.d(TAG, "scroll changed: " + this.getTop() + " "+t);
            if(t <= 0){
                onTop = true;
                //Log.d(TAG, "scroll top: " + t);
                super.onScrollChanged(l, t, oldl, oldt);
                return;
                // reaches the top end
            }
            onTop = false;
    
            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
            }
            super.onScrollChanged(l, t, oldl, oldt);
        }
    }
    
    0 讨论(0)
  • 2020-12-08 21:04

    you can use this code to detect is up or down scroll

    scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
                int lastScroll=0;
                @Override
                public void onScrollChanged() {
                    int scrollY = scrollView.getScrollY(); // For ScrollView herzontial use getScrollX()
    
                    if (scrollY > lastScroll ) {
                        Log.e("scroll","down scroll"+scrollY);
                    } else if (scrollY < lastScroll ) {
                        Log.e("scroll","up scroll"+scrollY);
                    }
                    lastScroll=scrollY;
                }
            });
    
    0 讨论(0)
  • 2020-12-08 21:05

    There is a much easier way than subclassing the ScrollView. The ViewTreeObserver object of the ScrollView can be used to listen for scrolls.

    Since the ViewTreeObserver object might change during the lifetime of the ScrollView, we need to register an OnTouchListener on the ScrollView to get it's ViewTreeObserver at the time of scroll.

    final ViewTreeObserver.OnScrollChangedListener onScrollChangedListener = new
                               ViewTreeObserver.OnScrollChangedListener() {
    
        @Override
        public void onScrollChanged() {
            //do stuff here 
        }
    };
    
    final ScrollView scrollView = (ScrollView) findViewById(R.id.scroller);
    scrollView.setOnTouchListener(new View.OnTouchListener() {
        private ViewTreeObserver observer;
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (observer == null) {
                observer = scrollView.getViewTreeObserver();
                observer.addOnScrollChangedListener(onScrollChangedListener);
            }
            else if (!observer.isAlive()) {
                observer.removeOnScrollChangedListener(onScrollChangedListener);
                observer = scrollView.getViewTreeObserver();
                observer.addOnScrollChangedListener(onScrollChangedListener);
            }
    
            return false;
        }
    });
    
    0 讨论(0)
  • 2020-12-08 21:08

    Actually there is a way to know how far the user has scrolled. The method getScrollY() from ScrollView tells you that.

    0 讨论(0)
提交回复
热议问题