Java Android-停止滚动后如何滚动到滚动视图的开始或结束?
我对java编程相当陌生,目前在Android Studio中使用ScrollView时遇到了一个问题。我希望scrollView在用户停止滚动后滚动到Begging或视图的末尾,具体取决于滚动停止的位置。我一直在尝试将setOnScrollChangeListener()与setOnTouchListener()结合使用,以检测滚动何时停止。这不起作用,因为一旦触摸启动,滚动将不起作用 我应该如何解决这个问题?或者,我是否应该使用其他更适合我的观点 我在这里找到了一个类似问题的老答案:Aleksandarf在这里使用了一个类。但我不知道该怎么叫,什么时候叫Java Android-停止滚动后如何滚动到滚动视图的开始或结束?,java,android,scrollview,onscrolllistener,Java,Android,Scrollview,Onscrolllistener,我对java编程相当陌生,目前在Android Studio中使用ScrollView时遇到了一个问题。我希望scrollView在用户停止滚动后滚动到Begging或视图的末尾,具体取决于滚动停止的位置。我一直在尝试将setOnScrollChangeListener()与setOnTouchListener()结合使用,以检测滚动何时停止。这不起作用,因为一旦触摸启动,滚动将不起作用 我应该如何解决这个问题?或者,我是否应该使用其他更适合我的观点 我在这里找到了一个类似问题的老答案:Alek
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;
}
}
提前谢谢 您好,我使用您在此处提供的链接和另一个问题( 创建名为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变量时做错了什么。我很高兴我的解决方案很有用:)关于你的下一个问题,你想列出一个列表吗?请接受我的解决方案,这样其他人也能找到它。不,它更像是一个新按钮和文本视图的页面滑块。我的程序中有一个textView,一旦滚动停止,它就需要更改,而我似乎不知道如何在onScrollEnd()函数中更改此文本。我找到了一个在用户完成滚动后更改textView的解决方案。向CustomScrollView类添加从MainActivity获取TextView的方法(u将在创建时调用MainActivity来设置TextView),然后在OnScrollend中添加并更新此TextView文本。希望它能帮上忙:)如果你还是不懂的话,问一个新问题,我会解决它(希望今天:))在谷歌搜索之后,我明白了,它现在可以工作了!非常感谢您的帮助:)