RecyclerView в NestedScrollView, чтобы заполнить оставшееся пространство - PullRequest
0 голосов
/ 27 ноября 2018

У меня есть иерархия представлений, как показано ниже: NestedScrollView> ConstraintLayout> [Layout, Layout, RecyclerView]

Я хочу, чтобы мой RecyclerView заполнил оставшееся пространство в Nested ScrollView.Мой ConstaintLayout имеет wrap_content layout_height.Дочерние макеты имеют фиксированную высоту, заданную в единицах измерения дп.И я хочу установить высоту RecyclerView, чтобы настроить оставшееся пространство внутри ConstraintLayout.

Я устанавливаю высоту ConstraintLayout программно до вычисленного значения, такого как высота обоих дочерних макетов + Высота экрана.Я почти работаю, но RecyclerView с текущей высотой wrap_content, кажется, выходит за пределы родительских границ ConstraintLayout, не вписываясь в его нижнее поле.Если я ограничу нижнюю часть родительского объекта ConstrintLayout, то он будет перемещен поверх содержимого дочерних макетов.Если я устанавливаю 0dp высоту RecyclerView, то он имеет 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 голосов
/ 27 ноября 2018

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