6

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

KiKo
  • 1,668
  • 7
  • 27
  • 56
  • No direct way. Maybe this will help you. [http://stackoverflow.com/questions/3948934/synchronise-scrollview-scroll-positions-android][1] [1]: http://stackoverflow.com/questions/3948934/synchronise-scrollview-scroll-positions-android – Nazgul Dec 04 '14 at 15:20

4 Answers4

5

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) 
tyczj
  • 71,600
  • 54
  • 194
  • 296
4

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;
  }
});
Criss
  • 755
  • 7
  • 22
4

I think I am little bit late to answer this but here is my answer

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

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 {

                }

    }
});