Android - 如何在滚动停止后滚动到 ScrollView 的开头或结尾?

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

我是 java 编程的新手,目前在 Android Studio 中我的 ScrollView 遇到了问题。我希望 scrollView 在用户停止滚动后滚动到视图的开头或结尾,具体取决于滚动停止的位置。我一直在尝试将 setOnScrollChangeListener() 与 setOnTouchListener() 结合使用来检测滚动何时停止。这不起作用,因为一旦启动触摸,滚动将不起作用。

我该如何解决这个问题?还是我应该使用其他一些视图,在我的情况下哪个更可取?

我在这里找到了类似问题的旧答案:Aleksandarf 的 Android: Detect when ScrollView stops scrolling,其中使用了 class。但我不明白如何或何时调用 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;
    }
} 

提前致谢!

您好,我使用您在此处提供的 link 和另一个问题构建了一个解决方案(Android: How to scroll ScrollView in top

创建一个 class 命名为 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
}}

在您的 xml 中添加:

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

有什么问题随时问:)