0

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!

ImNewOkay
  • 5
  • 3
  • this to listen to scrolling https://stackoverflow.com/questions/10713312/can-i-have-onscrolllistener-for-a-scrollview + this to scroll to where you want https://stackoverflow.com/questions/3080402/android-scrollview-force-to-bottom – Elias Fazel Feb 03 '20 at 15:27

1 Answers1

0

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 :)

Ben
  • 144
  • 2
  • 9
  • Yes it's working now! Thank you. I have a follow-up question though. Say I want to edit the text of the example textView within the CustomScrollView. How can I do this from the onScrollEnd() function? The app crashes when I try to manipulate the text. Im guessing I'm doing something wrong in the initialization of the textView variable within the class. – ImNewOkay Feb 03 '20 at 17:21
  • i am happy that my solution is useful :) about your next question, Are you trying to make a list? And please accept my solution so other also could find it . – Ben Feb 03 '20 at 17:44
  • No it's more like a page slider to new buttons and textViews. There is a textView in my program that needs to be changed once the scrolling has stopped and I can't seem to figure out how to change this text within the onScrollEnd() function. – ImNewOkay Feb 03 '20 at 18:47
  • i find a solution to changing textview when user finish scrolling. add method to CustomScrollView class that get TextView from MainActivity (u will call in MainActivity onCreate to set the textView) and add then update this TextView text in onScrollEnded. hope it help :) if you still don't understand it ask a new question and i will work on it(hopefully today:) ) – Ben Feb 03 '20 at 19:44
  • After some googling I understod and it's now working! Thank you so much for the help :) – ImNewOkay Feb 03 '20 at 20:46