NestedScrollView中的RecyclerView填充剩余空间

时间:2018-11-27 08:02:30

标签: android android-recyclerview android-constraintlayout android-nestedscrollview

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

我希望我的RecyclerView填充嵌套ScrollView中的剩余空间。 我的ConstaintLayout具有 wrap_content layout_height。子布局的固定高度设置为dp单位。我想设置RecyclerView高度来调整ConstraintLayout内的剩余空间。

我以编程方式将ConstraintLayout的高度设置为计算值,例如两个子Layouts的高度+ Screen Height。我几乎可以工作了,但是具有当前wrap_content高度的RecyclerView似乎超出了其父ConstraintLayout边界,不适合其底部边距。如果我约束到父ConstrintLayout的底部,则将其移到上面的子Layouts内容上。如果我将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>

1 个答案:

答案 0 :(得分:1)

android:fillViewport="true"上的

NestedScrollView强制内部ConstraintLayout填充剩余空间。您可以将底视图限制为ConstraintLayout的底部,也可以将RecyclerView的底部限制为底视图的顶部。在match_constraints上设置RecyclerView,以填充可用空间-类似于您的布局模型。

enter image description here

<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>