Android:ScrollView强制到底

Noa*_*man 185 android android-scrollview

我希望ScrollView能够从底部开始.任何方法?

hun*_*175 320

你应该在scroll.post中运行代码,如下所示:

scroll.post(new Runnable() {            
    @Override
    public void run() {
           scroll.fullScroll(View.FOCUS_DOWN);              
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 如果您不想专注于ScrollView,可以使用`scroll.scrollTo(0,scroll.getHeight());`跳转到底部,或`scroll.smoothScrollTo(0,scroll.getHeight()) ;`更顺畅的滚动 (7认同)
  • 有一些方法没有失去焦点当前元素,当我这样做时,我失去了我当前元素的焦点(与scrollview不同) (4认同)
  • 仅在布局尚未测量和布局的情况下才需要这样(例如,如果在onCreate中运行它).在按下按钮的情况下,不需要.post(). (2认同)
  • 在 Kotlin 中:`scrollView.post { scrollView.fullScroll(View.FOCUS_DOWN) }` (2认同)

Com*_*are 247

scroll.fullScroll(View.FOCUS_DOWN) 也应该工作.

  • 这似乎没有做任何事情,有什么建议吗?我在onCreate上试了一下,后来点击了按钮. (6认同)
  • 见下面的答案 (4认同)
  • 这个及以下,只有你给视图充分膨胀,只是简化ademar答案,才会有效.scrollView.postDelayed(new Runnable(){@Override public void run(){scrollView.fullScroll(View.FOCUS_UP // Or Down);}},1000); (2认同)

pea*_*ion 75

scroll.fullScroll(View.FOCUS_DOWN)将导致焦点的变化.当有多个可聚焦视图时,这将带来一些奇怪的行为,例如两个EditText.这个问题有另一种方式.

    View lastChild = scrollLayout.getChildAt(scrollLayout.getChildCount() - 1);
    int bottom = lastChild.getBottom() + scrollLayout.getPaddingBottom();
    int sy = scrollLayout.getScrollY();
    int sh = scrollLayout.getHeight();
    int delta = bottom - (sy + sh);

    scrollLayout.smoothScrollBy(0, delta);
Run Code Online (Sandbox Code Playgroud)

这很好用.

  • 这应该有更多的赞成.到目前为止,它是最好的答案. (5认同)

Aja*_*tha 41

有时scrollView.post不起作用

 scrollView.post(new Runnable() {
        @Override
        public void run() {
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        }
    });
Run Code Online (Sandbox Code Playgroud)

但是,如果你使用scrollView.postDelayed,它肯定会工作

 scrollView.postDelayed(new Runnable() {
        @Override
        public void run() {
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        }
    },1000);
Run Code Online (Sandbox Code Playgroud)


小智 35

对我来说最有效的是

scroll_view.post(new Runnable() {
     @Override
     public void run() {
         // This method works but animates the scrolling 
         // which looks weird on first load
         // scroll_view.fullScroll(View.FOCUS_DOWN);

         // This method works even better because there are no animations.
         scroll_view.scrollTo(0, scroll_view.getBottom());
     }
});
Run Code Online (Sandbox Code Playgroud)


ade*_*190 28

我增加工作完美.

    private void sendScroll(){
        final Handler handler = new Handler();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {Thread.sleep(100);} catch (InterruptedException e) {}
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        scrollView.fullScroll(View.FOCUS_DOWN);
                    }
                });
            }
        }).start();
    }
Run Code Online (Sandbox Code Playgroud)

注意

这个答案是一个真正旧版本的android的解决方法.今天postDelayed没有更多的bug,你应该使用它.

  • 为什么不使用scrollView.postDelayed(runnable,100)? (9认同)
  • 为了爱上帝,使用scrollView.postDelayed(runnable,100)!:) (4认同)
  • 不超过两个,但这在我的手机(LG JellyBean 4.1.2 Optimus G)中运行良好. (3认同)

小智 9

我试过了,成功了。

scrollView.postDelayed(new Runnable() {
    @Override
    public void run() {
        scrollView.smoothScrollTo(0, scrollView.getHeight());
    }
}, 1000);
Run Code Online (Sandbox Code Playgroud)