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

时间:2022-08-24 23:25:57

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滚动到底部?这意味着第一次显示时视图已滚动到底部。

9 个解决方案

#1


203  

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.

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

#2


43  

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);
    }
});

#3


25  

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);
            }
        });
    }
});

#4


16  

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">

#5


3  

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

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

yourScrollview.fullScroll(ScrollView.FOCUS_DOWN);

#6


3  

Try this

尝试这个

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

#7


2  

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

#8


1  

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

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

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

#9


1  

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);
                }
            });
        }
});

#1


203  

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.

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

#2


43  

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);
    }
});

#3


25  

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);
            }
        });
    }
});

#4


16  

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">

#5


3  

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

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

yourScrollview.fullScroll(ScrollView.FOCUS_DOWN);

#6


3  

Try this

尝试这个

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

#7


2  

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

#8


1  

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

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

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

#9


1  

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);
                }
            });
        }
});