Android scrollview onScrollChanged

前端 未结 11 1391
不思量自难忘°
不思量自难忘° 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: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);
                }
            }
        });
    

提交回复
热议问题