gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:36:02 25 4
gpt4 key购买 nike

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

我该如何解决这个问题?或者我应该使用其他一些 View 来代替,这对于我的情况来说更可取?

我在这里找到了类似问题的旧答案:Android: Detect when ScrollView stops scrolling由 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;
}
}

提前致谢!

最佳答案

您好,我使用您在此处提供的链接和另一个问题构建了一个解决方案( 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>

有什么问题都可以问:)

关于java - Android - 滚动停止后如何滚动到 ScrollView 的开头或结尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60042202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com