代码之家  ›  专栏  ›  技术社区  ›  Michał Ziobro

NestedScrollView中的RecyclerView以填充剩余空间

  •  0
  • Michał Ziobro  · 技术社区  · 7 年前

    我有如下视图层次结构: NestedScrollView>ConstraintLayout>[布局,布局,回收视图]

    我希望我的RecyclerView在嵌套的ScrollView中填充剩余空间。 我的建筑布局 包装内容 布局高度。子布局具有以dp单位设置的固定高度。我想设置RecyclerView高度来调整ConstraintLayout中的剩余空间。

    知道吗?

     <?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 回复  |  直到 7 年前
        1
  •  2
  •   Cheticamp    7 年前

    android:fillViewport="true" NestedScrollView 是在强迫内心 ConstraintLayout 约束布局 并约束 RecyclerView 在底部视图的顶部。套 match_constraints 回收服务 所以它填满了可用的空间-就像这个布局的模型。

    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>