Detect if ScrollView scrolls up or down - Android

I have one ScrollView

with one LinearLayout

with TextView

s. I want to detect when it ScrollView

scrolls up or down to hide / show ActionBar

.

+3


source to share


3 answers


you need to create a class that extends ScrollView

public class ExampleScrollView extends ScrollView 

      



and then an override onScrollChanged

which gives you the old and new scroll positions and from there you can determine which direction

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

      

+2


source


check it



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

      

+1


source


Correct 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 {

                }

    }
});

      

0


source







All Articles