以编程方式将视图添加到层次结构后,使用 ScrollView 滚动不起作用



我正在以编程方式将视图添加到嵌入在ScrollView中的LinearLayout中。但是,ScrollView没有考虑到扩展的高度,因此滚动将不起作用。添加视图后在滚动视图上调用invalidate()requestLayout()并不能解决问题。必须如何完成?

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/pager">
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp">
        <LinearLayout
            android:id="@+id/layout_to_which_views_are_added"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/label"> 

services.groupBy { it.category }.forEach { category ->
                val view = layoutInflater.inflate(R.layout.service_category, rootLayout, false)
                view.findViewById<TextView>(R.id.lblHeader).text = getStringArray(R.array.service_category_headers)[category.key]
                view.findViewById<TextView>(R.id.txtServices).text = category.value.joinToString(", ") { it.display }
                layout_to_which_views_are_added.addView(view)
            }
scrollView.invalidate()
scrollView.requestLayout()

发现问题,ScrollView 的父级布局,一个 CoordinatorLayout,本身由 ConstraintLayout 父级,缺少 bottomToBottom-to-parent 约束,这会将其拉伸到底部,然后使 ScrollView 可滚动到整个内容

最新更新