[英]Android: How to scroll ScrollView in top
I have two buttons that switching to the next list of events and the previous one.我有两个按钮可以切换到下一个事件列表和上一个事件列表。
When I go to next\previous event, scrolls remains somewhere below.当我 go 到下一个\上一个事件时,滚动仍然在下面的某个地方。 But I need to "rewind" it up.
但我需要“倒带”它。
I'm trying:我正在努力:
scrollViewEventDetails.pageScroll(ScrollView.FOCUS_UP);
and:和:
scrollViewEventDetails.scrollTo(0, 0);
but it doesn't work.但它不起作用。 Please, help!
请帮忙!
You shoud write next:你接下来应该写:
scrollViewEventDetails.fullScroll(View.FOCUS_UP);//if you move at the end of the scroll
scrollViewEventDetails.pageScroll(View.FOCUS_UP);//if you move at the middle of the scroll
This did not work for me:这对我不起作用:
scrollViewEventDetails.fullScroll(View.FOCUS_UP); //if you move at the end of the scroll
This worked for me:这对我有用:
scroll_view.smoothScrollTo(0,0);
scrollview.pageScroll(View.FOCUS_UP);
is more consistent and worked for me.更加一致并且为我工作。 :)
:)
The timing of these calls is crucial.这些电话的时机至关重要。 getViewTreeObserver().addOnGlobalLayoutListener didn't work for me but using the onWindowFocusChanged callback in my activity did:
getViewTreeObserver().addOnGlobalLayoutListener 对我不起作用,但在我的活动中使用 onWindowFocusChanged 回调可以:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
mScrollView.fullScroll(View.FOCUS_UP);
}
}
I know this is an old post but there is a reason the code will work some places and not others.我知道这是一篇旧文章,但代码在某些地方有效而不在其他地方有效是有原因的。 It will get interrupted by the main thread if called in the wrong place, especially if the ScrollView must scroll a large distance.
如果在错误的地方调用它会被主线程中断,特别是如果 ScrollView 必须滚动很长的距离。 Therefore put your call in its own thread.
因此,将您的电话放在自己的线程中。 Fixed.
固定的。
(new Thread(new Runnable(){
public void run(){
mScrollView.fullScroll(View.FOCUS_UP);
}
})).start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.