简体   繁体   English

Android:如何在顶部滚动 ScrollView

[英]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.

相关问题 如何滚动滚动视图以在Android顶部显示子视图 - How to scroll a scrollview to come a subview in top in Android android,当在viewpager中滚动recyclerview时,如何使父scrollview滚动到顶部 - android when recyclerview in a viewpager is scrolled, how to top parent scrollview scroll Android ScrollView如何在滚动到屏幕顶部时将组件保留在屏幕上? - Android ScrollView how to keep component on screen when it scroll to top of the screen? ScrollView滚动到顶部按钮点击android - ScrollView scroll to top on button click android Android 在滚动视图中滚动到顶部下一个替换片段 - Android scroll to top next replacing fragment in scrollview 如何滚动到长 ScrollView 布局的顶部? - How to scroll to top of long ScrollView layout? Android-到达scrollview的顶部后,滚动的一部分不应隐藏 - Android - Part of the scroll should not hide after reaching the top of the scrollview 添加新视图时,android scrollview滚动到顶部方向 - android scrollview scroll to top direction when new view added Android:如何强制ScrollView布局,然后滚动到一个点? - Android: how to force ScrollView layout, and then scroll to a point? 如何更改 ScrollView android 中实际滚动条的颜色? - how to change color of the actual scroll in ScrollView android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM