简体   繁体   English

Android ScrollView 滚动到底部

[英]Android ScrollView Scroll to Bottom

The ScrollView in Android is really confusing. Android 中的ScrollView真的很混乱。 I'm working on an app that adds a fragment of text to a TextView inside the ScrollView , and I want it to scroll to the bottom of the ScrollView .我正在开发一个将文本片段添加到ScrollView内的TextView的应用程序,我希望它滚动到ScrollView的底部。

I've tried these ways:我试过这些方法:

Scroll1.fullScroll(View.FOCUS_DOWN);

Scroll1.scrollTo(0, sv.getBottom());

Scroll1.scrollTo(0, Scroll1.getY() + Scroll1.getHeight());

NOTE: 1. Scroll1 is the ScrollView I used.注意: 1. Scroll1 是我使用的ScrollView 2. The first two methods can't scroll to the bottom. 2.前两种方法不能滚动到底部。 The last just doesn't work.最后一个不起作用。

How can I achieve this?我怎样才能做到这一点? Please help.请帮忙。 Thanks.谢谢。

try this it may helps if I know your problem: paste it in scroll view in xml试试这个,如果我知道你的问题,它可能会有所帮助:将它粘贴到 xml 的滚动视图中

android:fillViewport="true"

OR或者

in java activity在 Java 活动中

scrollView.setFillViewport(true);

use the following code once you want to update the scrollview:一旦要更新滚动视图,请使用以下代码:

scrollView.post(new Runnable() { 
     public void run() {
         scrollView.fullScroll(View.FOCUS_DOWN);
     }
 });

Your scrollView may not scroll to bottom in onCreate until you give some time to the view to inflate fully.您的 scrollView 可能不会在 onCreate 中滚动到底部,直到您给视图一些时间使其完全膨胀。

So to make it scroll...所以要让它滚动...

mScrollView.postDelayed(new Runnable() {
            @Override
            public void run() {
                View lastChild = mScrollView.getChildAt(mScrollView.getChildCount() - 1);
                int bottom = lastChild.getBottom() + mScrollView.getPaddingBottom();
                int sy = mScrollView.getScrollY();
                int sh = mScrollView.getHeight();
                int delta = bottom - (sy + sh);

                mScrollView.smoothScrollBy(0, delta);
            }
        },1000);

You can also use scrollView.fullScroll(View.FOCUS_DOWN);您也可以使用scrollView.fullScroll(View.FOCUS_DOWN); in run()在运行()

I combinde this from a lot of answers.我从很多答案中结合了这一点。 It finally helped me.它终于帮助了我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM