检测ScrollView是向上还是向下滚动 - Android

时间:2014-12-04 15:15:12

标签: android android-actionbar android-scrollview

我有一个ScrollView,其中一个LinearLayoutTextView。我想检测ScrollView向上或向下滚动以隐藏/显示ActionBar

4 个答案:

答案 0 :(得分:2)

您需要创建一个扩展ScrollView

的类
public class ExampleScrollView extends ScrollView 

然后覆盖onScrollChanged,它会为您提供旧的和新的滚动位置,并从那里可以检测到哪个方向

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

答案 1 :(得分:1)

检查出来

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

答案 2 :(得分:1)

我想我回答得有点晚了,但这是我的答案

 scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            float y = 0;

            @Override
            public void onScrollChanged() {
                if (scrollView.getScrollY() > y) {
                    Log.v("Message", "Scrolls Down");
                } else {
                    Log.v("Message", "Scrolls Up");
                }
                y = scrollView.getScrollY();
            }
        });

答案 3 :(得分:-2)

  

肯定简短的回答

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 {

                }

    }
});