【问题标题】:Android Recycler View - reverse layout inside Constraint LayoutAndroid Recycler View - 约束布局内的反向布局
【发布时间】:2019-10-23 20:10:25
【问题描述】:

我在ConstraintLayout 中有一个RecyclerView,它在顶部被限制在parent,在底部被EditText 限制。这很好用,但是一旦我将回收器设置为 reverseLayout=true,如果我的项目多于屏幕无法显示,则必须向下滚动底部的第一项才能完全看到。预览正是它在真实设备上发生的方式,让我觉得我在布局中遗漏了一些东西。

注意:

如果我从app:layout_constraintBottom_toTopOf="@id/feedMessageInput"app:layout_constraintBottom_toBottomOf="parent" 删除RecyclerViewEditText 的底部约束,则不会发生此问题,但是这是不可取的。

Xml 布局:

<?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:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="@dimen/margin_half"
    android:layout_marginTop="@dimen/margin_normal"
    android:layout_marginEnd="@dimen/margin_normal"
    android:layout_marginBottom="@dimen/margin_normal"
    android:background="@drawable/shp_rounded_rectangle_transparent_white_05"
    tools:context=".ui.messages.feed.FeedFragment">

    <ImageView
        android:id="@+id/feedEmptyImageView"
        android:layout_width="@dimen/icon_size_xxxxxlarge"
        android:layout_height="@dimen/icon_size_xxxxxlarge"
        android:contentDescription="@string/no_recipients_image"
        android:src="@drawable/ic_messages_124dp"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:visibility="visible" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/feedRecycler"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="@dimen/margin_triple"
        android:layout_marginEnd="@dimen/margin_half"
        android:scrollbars="vertical"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_constraintBottom_toTopOf="@id/feedMessageInput"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:reverseLayout="true"
        tools:itemCount="5"
        tools:listitem="@layout/row_message" />

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/feedRecipient"
        android:layout_width="@dimen/icon_size_medium"
        android:layout_height="@dimen/icon_size_medium"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="@dimen/margin_normal"
        app:layout_constraintTop_toTopOf="@id/feedMessageInput"
        app:layout_constraintBottom_toBottomOf="@id/feedMessageInputView"
        app:layout_constraintEnd_toStartOf="@id/feedMessageInputView"
        app:layout_constraintHorizontal_bias="0"
        tools:src="@tools:sample/avatars" />

    <View
        android:id="@+id/feedMessageInputView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/shp_rounded_rectangle_transparent_white_10"
        android:maxHeight="@dimen/message_background_input_max_height"
        app:layout_constraintBottom_toBottomOf="@id/feedMessageInput"
        app:layout_constraintEnd_toEndOf="@id/feedMessageInput"
        app:layout_constraintStart_toStartOf="@id/feedMessageInput"
        app:layout_constraintTop_toTopOf="@id/feedMessageInput" />

    <EditText
        android:id="@+id/feedMessageInput"
        style="@style/MessagingEditText.NoUnderline"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin_normal"
        android:importantForAutofill="no"
        android:inputType="textMultiLine"
        android:maxLength="@integer/max_message_length"
        android:padding="@dimen/margin_half"
        android:textSize="@dimen/message_input_text_size"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/feedSelectTemplate"
        app:layout_constraintHeight_max="@dimen/message_background_input_max_height"
        app:layout_constraintHeight_min="@dimen/message_background_input_height"
        app:layout_constraintStart_toEndOf="@id/feedRecipient"
        app:layout_constraintTop_toBottomOf="@id/feedRecycler"
        tools:hint="Send message to +447445343433"
        tools:ignore="LabelFor" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/feedSendMessage"
        style="@style/MessagingFloatingActionButton.Small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/margin_normal"
        android:src="@drawable/ic_sms_message_send_24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/feedSelectTemplate"
        app:layout_constraintTop_toTopOf="@id/feedMessageInput" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/feedSelectTemplate"
        style="@style/MessagingFloatingActionButton.Small.Contrast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/margin_normal"
        android:src="@drawable/ic_sms_message_template_24dp"
        app:layout_constraintEnd_toStartOf="@id/feedSendMessage"
        app:layout_constraintStart_toEndOf="@id/feedMessageInput"
        app:layout_constraintTop_toTopOf="@id/feedMessageInput" />

</android.support.constraint.ConstraintLayout>

当前多行从顶部和底部滚动时的预览不正确 - 这是不正确的:

我希望reverseLayout=true 在顶部滚动多少行时都能正常工作:

任何我可能错过的东西的帮助将不胜感激。

【问题讨论】:

  • 尝试将 RecyclerView 的宽度从 match_parent 更改为 0dp (match_constraints)。那是我为你工作的。
  • @Cheticamp 嗨,感谢您的评论。我可以确认这确实有效!如果你可以写作为答案(如果可能的话,你的理由为什么这样做)我会接受作为答案。我仍然不明白从0dpmatch_parent 的宽度变化的要求,以及它如何影响高度,以及为什么这只需要反向布局。

标签: android xml android-recyclerview android-constraintlayout


【解决方案1】:

根据documentation for ConstraintLayout

重要提示:不建议将 MATCH_PARENT 用于 ConstraintLayout 中包含的小部件。类似的行为可以通过使用 MATCH_CONSTRAINT 来定义,并将相应的左/右或上/下约束设置为“父级”。

为什么这样的工作?我的猜测是它并没有得到真正的支持,但没有被禁用,可能没有经过测试。简短的回答是专门使用match_constraints (0dp) 并在涉及ConstraintLayout 时始终避免使用match_parent

【讨论】:

    【解决方案2】:

    面临同样的问题。它看起来像 ConstraintLayout 问题(仍处于测试阶段)。 我已经重写了 xml 布局以使用 LinearLayout,它对我有用。 但仍在等待 Google 的修复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多