【发布时间】:2018-11-27 08:02:29
【问题描述】:
我有如下视图层次结构: NestedScrollView > ConstraintLayout > [Layout,Layout, RecyclerView]
我希望我的 RecyclerView 填充 Nested ScrollView 中的剩余空间。 我的 ConstaintLayout 有 wrap_content layout_height。子布局具有以 dp 为单位设置的固定高度。我想设置 RecyclerView 高度来调整 ConstraintLayout 内的剩余空间。
我以编程方式将 ConstraintLayout 的高度设置为计算值,例如两个子布局的高度 + 屏幕高度。我几乎可以工作,但当前 wrap_content 高度的 RecyclerView 似乎超出了其父 ConstraintLayout 边界,不适合其底部边距。如果我约束到父 ConstrintLayout 的底部,那么它将移动到上面的子布局内容上。如果我设置 RecyclerView 的 0dp 高度,那么它的 0dp 高度不会在可用空间内拉伸。也许唯一的选择是以编程方式将 RecyclerView 的高度设置为固定的 dp 大小。 onMeasure()、onLayout 还是 Views、Fragments 等中的其他回调方法?
有什么想法吗?
<?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>
【问题讨论】:
-
你能发布 XML 代码吗?
-
如果我将 RecyclerView 的 layout_height 修改为固定的 100dp,则回收器视图将正确显示,即在 ConstraintLayout 内其下方有 100dp 和 16dp 的边距。但是设置 wrap_content 或 0dp 以填充父 ConstraintLayout 内的可用空间不起作用。我正在将 ConstraintLayout 的 BillingNestedView 内的高度配置为固定大小,具体取决于设备的大小
标签: android android-recyclerview android-constraintlayout android-nestedscrollview