NestedScrollView中的RecyclerView填充剩余空间

Mic*_*bro 0 android android-recyclerview android-nestedscrollview android-constraintlayout

我的视图层次结构如下: NestedScrollView > ConstraintLayout > [Layout,Layout, RecyclerView]

我希望我的 RecyclerView 填充 Nested ScrollView 中的剩余空间。我的ContaintLayout有wrap_content布局_高度。子布局具有以 dp 单位设置的固定高度。我想设置 RecyclerView 高度来调整 ConstraintLayout 内的剩余空间。

我以编程方式将 ConstraintLayout 的高度设置为计算值,例如两个子布局的高度 + 屏幕高度。我几乎可以工作,但是具有当前wrap_content高度的RecyclerView似乎超出了其父ConstraintLayout边界,不适合其底部边距。如果我约束到父 ConstrintLayout 的底部,那么它将移动到上面的子布局内容上。如果我设置 RecyclerView 的 0dp 高度,那么它的高度为 0dp,不会拉伸到可用空间内。也许唯一的选择是以编程方式将 RecyclerView 的高度设置为固定 dp 大小。onMeasure()、onLayout 或视图、片段等中的其他回调方法?

任何想法?

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:myapp="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".ui.billing.BillingFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <com.domain.AppName.base.ui.billing.BillingNestedScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"

        android:overScrollMode="never"
        android:fillViewport="true"

        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"

        android:background="@color/theMinteFormBackground">
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true">

            <com.domain.AppName.base.utils.design.ShadowLayout
                android:id="@+id/creditCardSectionLayout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                myapp:sl_shadow_color="#AAD4D4D4"
                myapp:sl_shadow_angle="360"
                myapp:sl_shadow_distance="0dp"
                myapp:sl_shadow_radius="4dp"
                myapp:sl_shadow_top="true"
                myapp:sl_shadow_bottom="true"
                myapp:sl_shadow_right="true"
                myapp:sl_shadow_left="true"
                myapp:sl_shadowed="true">
                <android.support.constraint.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/white">

                    <com.domain.AppName.base.ui.forms.FormHeaderView
                        android:id="@+id/creditCardHeaderFormView"
                        android:layout_width="0dp"
                        android:layout_height="50dp"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        myapp:labelText="@string/billing_payment_info_section_header"
                        style="@style/FormSectionHeaderStyle" />

                    <Button
                        android:id="@+id/cancelCreditCardButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/payment_info_form_cancel_button_title"
                        app:layout_constraintEnd_toEndOf="@id/creditCardHeaderFormView"
                        app:layout_constraintTop_toTopOf="@id/creditCardHeaderFormView"
                        app:layout_constraintBottom_toBottomOf="@id/creditCardHeaderFormView"
                        style="@style/CancelCreditCardButtonStyle"
                        android:visibility="invisible" />

                    <Button
                        android:id="@+id/scanCreditCardButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/payment_info_form_scan_card_button_title"
                        app:layout_constraintEnd_toEndOf="@id/creditCardHeaderFormView"
                        app:layout_constraintTop_toTopOf="@id/creditCardHeaderFormView"
                        app:layout_constraintBottom_toBottomOf="@id/creditCardHeaderFormView"
                        style="@style/ScanCreditCardButtonStyle" />

                    <com.domain.AppName.base.ui.forms.material.ValidableCardNumberInput
                        android:id="@+id/cardNumberInput"
                        android:layout_width="0dp"
                        android:layout_height="50dp"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/creditCardHeaderFormView"
                        android:layout_marginStart="16dp"
                        android:layout_marginEnd="16dp"
                        android:layout_marginTop="16dp"

                        myapp:hintText="@string/payment_info_form_card_number_label"
                        myapp:inputType="number"
                        myapp:inputText=""
                        myapp:iconDrawable="@drawable/baseline_credit_card_24"

                        myapp:isRequired="true"
                        myapp:validationEmptyError="@string/validation_card_number_empty"
                        myapp:requireType="none"
                        myapp:validationError="@string/validation_card_number_error"
                        myapp:validationErrorColor="@color/theMinteValidationError" />

                    <com.domain.AppName.base.ui.forms.material.ValidableExpiryDateInput
                        android:id="@+id/expirationDateInput"
                        android:layout_width="0dp"
                        android:layout_height="50dp"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/cardNumberInput"
                        android:layout_marginStart="16dp"
                        android:layout_marginEnd="16dp"
                        android:layout_marginTop="16dp"

                        myapp:hintText="@string/payment_info_form_expiration_date_label"
                        myapp:inputType="number"
                        myapp:inputText=""

                        myapp:isRequired="true"
                        myapp:validationEmptyError="@string/validation_expiration_date_empty"
                        myapp:requireType="regex"
                        myapp:regexPattern="\\d{1,2}/\\d{2,4}"
                        myapp:validationError="@string/validation_expiration_date_error"
                        myapp:validationErrorColor="@color/theMinteValidationError" />

                    <com.domain.AppName.base.ui.forms.material.ValidableTextInput
                        android:id="@+id/cvcTextInput"
                        android:layout_width="0dp"
                        android:layout_height="50dp"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@id/expirationDateInput"
                        app:layout_constraintBottom_toBottomOf="parent"
                        android:layout_margin="16dp"

                        myapp:hintText="@string/payment_info_form_cvc_label"
                        myapp:inputType="number"
                        myapp:inputText=""

                        myapp:isRequired="true"
                        myapp:validationEmptyError="@string/validation_cvc_empty"
                        myapp:requireType="none"
                        myapp:minLength="3"
                        myapp:maxLength="4"
                        myapp:validationError="@string/validation_cvc_error"
                        myapp:validationErrorColor="@color/theMinteValidationError" />
                </android.support.constraint.ConstraintLayout>
            </com.domain.AppName.base.utils.design.ShadowLayout>

            <include
                android:id="@+id/googlePayFormView"
                layout="@layout/view_google_pay"
                android:layout_width="0dp"
                android:layout_height="82dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/creditCardSectionLayout" />

           <android.support.v7.widget.RecyclerView
               android:id="@+id/recyclerView"
               android:layout_width="0dp"
               android:layout_height="100dp"
               android:layout_margin="16dp"

               android:nestedScrollingEnabled="true"


               app:layout_constraintStart_toStartOf="parent"
               app:layout_constraintEnd_toEndOf="parent"
               app:layout_constraintTop_toBottomOf="@id/googlePayFormView" />

            <View
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_marginTop="16dp"

                app:layout_constraintTop_toBottomOf="@id/recyclerView"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

        </android.support.constraint.ConstraintLayout>
    </com.domain.AppName.base.ui.billing.BillingNestedScrollView>

    </android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

Che*_*amp 7

android:fillViewport="true"NestedScrollView是迫使内部ConstraintLayout填充剩余空间。您可以将底部视图约束到底部视图ConstraintLayout,并将底部视图约束RecyclerView到底部视图的顶部。设置match_constraintsRecyclerView填充可用空间 - 就像布局的模型一样。

在此输入图像描述

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.core.widget.NestedScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/white"
        android:fillViewport="true"
        app:layout_constraintBottom_toBottomOf="parent"
        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">

            <FrameLayout
                android:id="@+id/creditCardSectionLayout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                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="250dp"
                    android:background="@android:color/darker_gray" />
            </FrameLayout>

            <FrameLayout
                android:id="@+id/googlePayFormView"
                android:layout_width="0dp"
                android:layout_height="82dp"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:background="@android:color/holo_green_light"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/creditCardSectionLayout" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_margin="16dp"
                android:layout_marginBottom="8dp"
                android:background="@android:color/holo_red_light"
                app:layout_constraintBottom_toTopOf="@+id/view2"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/googlePayFormView" />

            <View
                android:id="@+id/view2"
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_marginBottom="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)