Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any way to identify scrollview scrolling up or down in android?

Hey just wanted to identify whether a scrollview is scrolling up or down in android .Wanted to do different actions while scrolling up and scrolling down.

Any idea is appreciated, thanks

like image 966
Aswathy Sujatha Avatar asked Jan 09 '15 12:01

Aswathy Sujatha


People also ask

How do I know if my scroll is up or down?

When the page scroll happens, compare the previous scrollY value with the current scrollY . If the current value is greater than the old value, then the scroll direction is downwards. Otherwise, the scroll direction is upwards. Update the current scroll position to the variable.

Which way is scroll down?

Pressing page up scrolls up one page or gets you to the top of the page. Pressing page down scrolls down one page at a time or gets you to the bottom of the page. Some people may also refer to the page up and page down keys as the scroll keys, not to be confused with the scroll lock key.

What is fillViewport in ScrollView?

android:fillViewport. Defines whether the scrollview should stretch its content to fill the viewport.

What is nested scroll view?

NestedScrollView is just like ScrollView , but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.


1 Answers

Try this: Extends Scrollview and override onTouchEvent method

private float mTouchPosition;
private float mReleasePosition;

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        mTouchPosition = event.getY();
    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        mReleasePosition = event.getY();

        if (mTouchPosition - mReleasePosition > 0) {
            // user scroll down
        } else {
            //user scroll up
        }
    }
    return super.onTouchEvent(event);
}
like image 192
Felipe Pozueco Zaffari Avatar answered Oct 27 '22 00:10

Felipe Pozueco Zaffari