【问题标题】:Android - Auto Scroll LinearLayout inside ScrollView?Android - 滚动视图内的自动滚动线性布局?
【发布时间】:2018-07-17 18:55:38
【问题描述】:

我在 ScrollView 中有一个垂直的 LinearLayout。 LinearLayout 动态增长。每当按下按钮(即在滚动视图之外)时,我都会向此 LinearLayout 添加片段,添加片段,我也可以看到 scollbar 但我希望滚动条自动滚动到我添加的最后一个元素。目前没有自动滚动,我必须手动滚动才能看到最后一个元素。

<ScrollView
    android:id="@+id/scrollView2"
    android:layout_width="557dp"
    android:layout_height="440dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView16"
    app:layout_constraintVertical_bias="0.131"
    >

    <LinearLayout
        android:id="@+id/linear_layout_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

    </LinearLayout>
</ScrollView>

【问题讨论】:

  • 首先使用recyclerview而不是scrollview,这给了你更多的选择,然后我认为最好添加片段或其他布局作为recyclerview项目

标签: android scrollview android-linearlayout


【解决方案1】:

请使用 recyclerView 而不是 scrollView。 这是使用 scrollView 的示例。

mScrollView.fullScroll(ScrollView.FOCUS_DOWN);

mScrollView.postDelayed(new Runnable() {
@Override
public void run() {
    scroll.fullScroll(ScrollView.FOCUS_DOWN);
}}, 700);

【讨论】:

  • 谢谢,它的工作原理。但我可以知道为什么需要 postDelayed 吗?不使用 postDelayed 时它不起作用。它带我到最后一行,但现在是最后一行(不使用 postDelayed 时)
  • 亲爱的,我很高兴.. 我们使用后期延迟,因为加载函数需要一些时间,应该暂停一下才能获得准确的结果。所以,这就是我们使用后期延迟的原因
【解决方案2】:

您可以在添加片段时自动滚动。

scrollView.smoothScrollTo(0,scrollView.getBottom);

【讨论】:

  • 它向下滚动但不是最后一行,而是转到倒数第二行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多