Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if a ScrollView is scrolling up or down - Android

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

like image 622
KiKo Avatar asked Dec 04 '14 15:12

KiKo


People also ask

How can I tell if ScrollView is scrolling Android?

Do you know if it is possible to know if an Android Widget ScrollView can scroll? If it has enough space it doesn't need to scroll, but as soon as a dimension exceeds a maximum value the widget can scroll.

How do I know my RecyclerView is scrolling?

You can use Scroll listener to detect scroll up or down changes in RecyclerView . RecycleView invokes the onScrollStateChanged() method before onScrolled() method. The onScrollStateChanged() method provides you the RecycleView's status: SCROLL_STATE_IDLE : No scrolling.

What is the difference between ListView and ScrollView?

ScrollView is used to put different or same child views or layouts and the all can be scrolled. ListView is used to put same child view or layout as multiple items. All these items are also scrollable. Simply ScrollView is for both homogeneous and heterogeneous collection.


3 Answers

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) 
like image 159
tyczj Avatar answered Sep 23 '22 23:09

tyczj


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;
  }
});
like image 33
Criss Avatar answered Sep 26 '22 23:09

Criss


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();
            }
        });
like image 36
hernan gonzalez Avatar answered Sep 25 '22 23:09

hernan gonzalez