Detect if a ScrollView is scrolling up or down - Android

自闭症网瘾萝莉.ら 提交于 2020-01-01 06:48:25

问题


I have one ScrollView with one LinearLayout with TextViews. I want to detect when ScrollView is scrolling up or down to hide/show ActionBar.


回答1:


you need to create a class that extends ScrollView

public class ExampleScrollView extends ScrollView 

and then Override onScrollChanged that gives you the old and new scroll positions and from there you can detect which direction

protected void onScrollChanged(int l, int t, int oldl, int oldt) 



回答2:


check this out

scrollView.setOnTouchListener(new View.OnTouchListener() {
  float y0 = 0;
  float y1 = 0;

  @Override
  public boolean onTouch(View view, MotionEvent motionEvent) {

    if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
      y0 = motionEvent.getY();
      if (y1 - y0 > 0) {
        Log.i("Y", "+");
        AnimateFloat(true);
      } else if (y1 - y0 < 0) {
        Log.d("Y", "-");
        AnimateFloat(false);
      }
      y1 = motionEvent.getY();
    }

    return false;
  }
});



回答3:


A sure short answer

scroll.setOnScrollChangeListener(new View.OnScrollChangeListener() {
    @Override
    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {


                int x = scrollY - oldScrollY;
                if (x > 0) {
                    //scroll up
                } else if (x < 0) {
                    //scroll down
                } else {

                }

    }
});


来源:https://stackoverflow.com/questions/27297467/detect-if-a-scrollview-is-scrolling-up-or-down-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!