Search code examples
javaandroidscrollviewonscrolllistener

Android - How to Scroll to Begining or end of ScrollView after Scrolling Stopped?


I'm rather new to java programming and is curently facing a problem with my ScrollView in Android Studio. I would like for the scrollView to either scroll to the beggining or the end of the view after the user has stopped scrolling, depending on where the scrolling is stopped. I've been trying out the setOnScrollChangeListener() combined with the setOnTouchListener() to detect when the scrolling has been stopped. This doesn't work because once the touch is initiated the scrolling won't function.

How should I tackle this problem? Or is there some other view I should use instead which would be more preferable in my case?

I found an old answer to a similar problem here: Android: Detect when ScrollView stops scrolling by Aleksandarf where a class is being used. But I don't understand how or when to call the class.

public class ScrollViewWithOnStopListener extends ScrollView {

    OnScrollStopListener listener;

    public interface OnScrollStopListener {
        void onScrollStopped(int y);
    }

    public ScrollViewWithOnStopListener(Context context) {
        super(context);
    }

    public ScrollViewWithOnStopListener(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
                checkIfScrollStopped();
        }

        return super.onTouchEvent(ev);
    }

    int initialY = 0;

    private void checkIfScrollStopped() {
        initialY = getScrollY();
        this.postDelayed(new Runnable() {
            @Override
            public void run() {
                int updatedY = getScrollY();
                if (updatedY == initialY) {
                    //we've stopped
                    if (listener != null) {
                        listener.onScrollStopped(getScrollY());
                    }
                } else {
                    initialY = updatedY;
                    checkIfScrollStopped();
                }
            }
        }, 50);
    }

    public void setOnScrollStoppedListener(OnScrollStopListener yListener) {
        listener = yListener;
    }
} 

Thanks in advance!


Solution

  • Hi i built a solution using the link you provide here and another question(Android: How to scroll ScrollView in top.

    Create a class named CustomScrollView :

    public class CustomScrollView extends ScrollView {
    private long lastScrollUpdate = -1;
    
    public CustomScrollView(Context context) { super(context);}
    public CustomScrollView(Context context, AttributeSet attrs) { super(context,attrs);}
    public CustomScrollView (Context context, AttributeSet attrs, int default_style){super(context, attrs, default_style);}
    
    private class ScrollStateHandler implements Runnable {
    
        @Override
        public void run() {
            long currentTime = System.currentTimeMillis();
            if ((currentTime - lastScrollUpdate) > 100) {
                lastScrollUpdate = -1;
                onScrollEnd();
            } else {
                postDelayed(this, 100);
            }
        }
    }
    
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (lastScrollUpdate == -1) {
            onScrollStart();
            postDelayed(new ScrollStateHandler(), 100);
        }
    
        lastScrollUpdate = System.currentTimeMillis();
    }
    
    private void onScrollStart() {
        // Feel Free to add something here
    }
    
    private void onScrollEnd() {
        this.fullScroll(View.FOCUS_UP); // Each time user finish with scrolling it will scroll to top
    }}
    

    in your xml add:

    <YOUR-PACKAGE-NAME.CustomScrollView
    android:id="@+id/scrollMe"
    android:layout_width="match_parent"
    android:layout_height="100dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="tEST\nSCROLL DOWN\n\n\n\nHello World!\n test \n test \nSCROLL\n"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    </YOUR-PACKAGE-NAME.CustomScrollView>
    

    Feel free to ask anything :)