Android scrollview onScrollChanged

前端 未结 11 1387
不思量自难忘°
不思量自难忘° 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 20:43

    Starting from API 23 Android M, you can use OnScrollChangeListener.

     scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                //work with parameters
            }
        });
    
    0 讨论(0)
  • 2020-12-08 20:45

    No.

    ScrollView doesn't provide a listener for scroll events or even a way to check how far down the user has scrolled, so you have to do what is suggested by the link.

    0 讨论(0)
  • 2020-12-08 20:47

    you can use this trigger to show Toast or Start Activity

     scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                 // do something when Scroll  
            }
        });
    
    0 讨论(0)
  • 2020-12-08 20:49

    Just use NestedScroll and NestedScroll.setOnScrollChangeListener().

    0 讨论(0)
  • 2020-12-08 20:57

    NestedScrollView is just like android.widget.ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android.

    1st - Use NestedScrollView binding instead ScrollView

    2nd - Set Scroll listener, like this, to detect axis Y moving for a headerView by example

        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                Log.d(TAG, "onScrollChangeForY - scrollY: " + scrollY + " oldScrollY: " + oldScrollY);
    
                int MOVE = -1, SCROLL_UP = 0, SCROLL_DOWN = 1;
                float initialPositionY = headerView.getY();
    
                MOVE = scrollY > oldScrollY ? SCROLL_UP : SCROLL_DOWN;
    
                if (MOVE == SCROLL_UP) {
    
                    int incrementY = scrollY - oldScrollY;
    
                    headerView.setY(initialPositionY - incrementY);
    
                } else {
    
                    int incrementY = oldScrollY - scrollY;
    
                    headerView.setY(initialPositionY + incrementY);
                }
            }
        });
    
    0 讨论(0)
  • 2020-12-08 20:58

    There is ready for use component, which helps to listen to scroll events of arbitrary views in Android. Internally this component adds ViewTreeObserver scroll events listener on devices with old Android API (similar as proposed in Shubhadeep Chaudhuri's answer) and View scroll events listener on devices with new Android API (API level 23+).

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