代码之家  ›  专栏  ›  技术社区  ›  Chad Schultz

ConstraintLayout中的ScrollView内的非滚动包装内容回收服务

  •  0
  • Chad Schultz  · 技术社区  · 7 年前

    我想记录我遇到的问题和解决方案,以造福于其他人,并在我的解决方案中找到关键缺陷的帮助。

    我想要一个 RecyclerView 在一个布局中包含任意数量的行和其他多个视图。这个 回收服务 View s应该在 ScrollView 那是卷轴,但是 回收服务 它本身不应该滚动。

    回收服务 是未知的,我需要在 回收服务 match_parent .

    我遇到了一些奇怪的问题:当我更新 回收服务 数据(使用 AsyncListDiffer )用户界面应该更新,整个 会跳到被约束的视图之上,直接跳到父视图的顶部。这不是怎么 ConstraintLayout 应该有规矩的。

    在下面 回收服务 会消失——一旦 回收服务 数据更新。

    1. 回收服务 查看 s在a里面 约束布局 卷轴视图 (或 NestedScrollView
    2. 设置 回收服务 高度 wrap_content app:layout_constrainedHeight="true" 没有伤害)
    3. 有固定的高度 包装内容

    这对我来说很有帮助,因为我的头撞到了墙上,尝试了一些不起作用的解决方案: make RecyclerView's height to "wrap_content" in Constraint layout

    这个解决方案非常简单。布局可以类似于:

        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <!-- other Views -->
    
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/otherView"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintBottom_toTopOf="@+id/anotherView"
                    />
    
                    <!-- other Views -->
            </androidx.constraintlayout.widget.ConstraintLayout>
    
        </ScrollView>
    

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                       xmlns:tools="http://schemas.android.com/tools"
                                                       xmlns:app="http://schemas.android.com/apk/res-auto"
                                                       android:id="@+id/constraintLayout"
                                                       android:layout_width="match_parent"
                                                       android:layout_height="?attr/listPreferredItemHeight">
    
    <!-- other views -->
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    问题是:我不喜欢固定高度的排。如果其他内容需要进入呢?如果用户更改了文本大小怎么办?我更喜欢能适应大小的灵活布局。但如果我这么做了 在滚动视图中占据屏幕的高度,将所有较低的视图从屏幕底部推下,不管它包含的行数有多少。

    我能想到的其他办法是 查看 s在外面 成为 ,或避免 回收服务 全部并以编程方式添加 s到a LinearLayout . 这些方法更难看。

    回收服务 可以有一个高度 包装内容

    0 回复  |  直到 7 年前