【问题标题】:Android - How to Scroll to Begining or end of ScrollView after Scrolling Stopped?Android - 滚动停止后如何滚动到 ScrollView 的开头或结尾?
【发布时间】:2020-05-19 09:13:58
【问题描述】:

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

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

我在这里找到了一个类似问题的旧答案:Android: Detect when ScrollView stops scrolling by Aleksandarf,其中使用了一个类。但我不明白如何或何时调用课程。

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

提前致谢!

【问题讨论】:

标签: java android scrollview onscrolllistener


【解决方案1】:

您好,我使用您在此处提供的链接和另一个问题(Android: How to scroll ScrollView in top.

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

随便问吧:)

【讨论】:

  • 是的,它现在正在工作!谢谢你。不过,我有一个后续问题。假设我想在 CustomScrollView 中编辑示例 textView 的文本。如何从 onScrollEnd() 函数执行此操作?当我尝试操作文本时应用程序崩溃。我猜我在类中初始化 textView 变量时做错了。
  • 我很高兴我的解决方案很有用 :) 关于您的下一个问题,您是否要列一个清单?请接受我的解决方案,以便其他人也能找到它。
  • 不,它更像是新按钮和 textViews 的页面滑块。一旦滚动停止,我的程序中有一个 textView 需要更改,我似乎无法弄清楚如何在 onScrollEnd() 函数中更改此文本。
  • 我找到了在用户完成滚动时更改文本视图的解决方案。将方法添加到从 MainActivity 获取 TextView 的 CustomScrollView 类(您将在 MainActivity onCreate 中调用以设置 textView),然后在 onScrollEnded 中添加然后更新此 TextView 文本。希望对您有所帮助:)如果您仍然不明白,请提出一个新问题,我会继续努力(希望今天:))
  • 经过一番谷歌搜索后,我明白了,它现在可以工作了!非常感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 2011-09-04
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多