Android 如何在活动启动时滚动到 ScrollView 的底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3307267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:38:03  来源:igfitidea点击:

How to scroll to bottom in a ScrollView on activity startup

androidscroll

提问by Palo

I am displaying some data in a ScrollView. On activity startup (method onCreate) I fill the ScrollView with data and want to scroll to the bottom.

我在 ScrollView 中显示一些数据。在活动启动(方法 onCreate)时,我用数据填充 ScrollView 并希望滚动到底部。

I tried to use getScrollView().fullScroll(ScrollView.FOCUS_DOWN). This works when I make it as an action on button click but it doesn't work in the onCreate method.

我尝试使用getScrollView().fullScroll(ScrollView.FOCUS_DOWN). 当我将其作为按钮单击操作时,这有效,但在 onCreate 方法中不起作用。

Is there any way how to scroll the ScrollView to the bottom on activity startup? That means the view is already scrolled to the bottom when first time displayed.

有什么方法可以在活动启动时将 ScrollView 滚动到底部?这意味着第一次显示时视图已经滚动到底部。

回答by Palo

It needs to be done as following:

需要按照以下步骤进行:

    getScrollView().post(new Runnable() {

        @Override
        public void run() {
            getScrollView().fullScroll(ScrollView.FOCUS_DOWN);
        }
    });

This way the view is first updated and then scrolls to the "new" bottom.

这样,视图首先更新,然后滚动到“新”底部。

回答by Harshid

Put the following code after your data is added:

添加数据后输入以下代码:

final ScrollView scrollview = ((ScrollView) findViewById(R.id.scrollview));
scrollview.post(new Runnable() {
    @Override
    public void run() {
        scrollview.fullScroll(ScrollView.FOCUS_DOWN);
    }
});

回答by sonida

This works for me:

这对我有用:

scrollview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        scrollview.post(new Runnable() {
            public void run() {
                scrollview.fullScroll(View.FOCUS_DOWN);
            }
        });
    }
});

回答by Tam Dao

You can do this in layout file:

您可以在布局文件中执行此操作:

                android:id="@+id/listViewContent"
                android:layout_width="wrap_content"
                android:layout_height="381dp" 
                android:stackFromBottom="true"
                android:transcriptMode="alwaysScroll">

回答by Makvin

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

回答by F.O.O

Try this

尝试这个

    final ScrollView scrollview = ((ScrollView) findViewById(R.id.scrollview));
    scrollview.post(new Runnable() {
       @Override
       public void run() {
         scrollview.fullScroll(ScrollView.FOCUS_DOWN);
       }
    });

回答by LEMUEL ADANE

Right after you append data to the view add this single line:

在将数据附加到视图后,立即添加以下一行:

yourScrollview.fullScroll(ScrollView.FOCUS_DOWN);

回答by rana

This is the best way of doing this.

这是最好的方法。

scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            scrollView.post(new Runnable() {
                @Override
                public void run() {
                    scrollView.fullScroll(View.FOCUS_DOWN);
                }
            });
        }
});

回答by Ramzy Hassan

After initializing your UI component and fill it with data. add those line to your on create method

初始化 UI 组件并填充数据后。将这些行添加到您的创建方法中

Runnable runnable=new Runnable() {
        @Override
        public void run() {
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        }
    };
    scrollView.post(runnable);