我想要一个ScrollView从底部开始。任何方法吗?


当前回答

在这种情况下,我们只使用滚动。scrollTo(0, sc.getBottom())不工作,使用它使用scroll.post

例子:

scroll.post(new Runnable() {
  @Override
  public void run() {
  scroll.fullScroll(View.FOCUS_DOWN);
  } 
});

其他回答

需要考虑的一件事是不设置什么。确保您的子控件,特别是EditText控件,没有RequestFocus属性集。这可能是布局中最后解释的属性之一,它将覆盖其父(布局或ScrollView)上的重力设置。

这里有一些滚动到底部的其他方法

fun ScrollView.scrollToBottom() {
    // use this for scroll immediately
    scrollTo(0, this.getChildAt(0).height) 

    // or this for smooth scroll
    //smoothScrollBy(0, this.getChildAt(0).height)

    // or this for **very** smooth scroll
    //ObjectAnimator.ofInt(this, "scrollY", this.getChildAt(0).height).setDuration(2000).start()
}

使用

如果你的滚动视图已经布局

my_scroll_view.scrollToBottom()

如果你的滚动视图没有完成布局(例如:你滚动到底部的活动onCreate方法…)

my_scroll_view.post { 
   my_scroll_view.scrollToBottom()          
}

我尝试成功了。

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

实际上,我发现调用fullScroll两次是有效果的:

myScrollView.fullScroll(View.FOCUS_DOWN);

myScrollView.post(new Runnable() {
    @Override
    public void run() {
        myScrollView.fullScroll(View.FOCUS_DOWN);
    }
});

这可能与在执行第一次(不成功的)滚动之后激活post()方法有关。我认为这种行为发生在任何之前的方法调用myScrollView之后,所以你可以尝试用其他任何可能与你相关的方法来替换第一个fullScroll()方法。

在这种情况下,我们只使用滚动。scrollTo(0, sc.getBottom())不工作,使用它使用scroll.post

例子:

scroll.post(new Runnable() {
  @Override
  public void run() {
  scroll.fullScroll(View.FOCUS_DOWN);
  } 
});