【发布时间】:2019-10-23 20:10:25
【问题描述】:
我在ConstraintLayout 中有一个RecyclerView,它在顶部被限制在parent,在底部被EditText 限制。这很好用,但是一旦我将回收器设置为 reverseLayout=true,如果我的项目多于屏幕无法显示,则必须向下滚动底部的第一项才能完全看到。预览正是它在真实设备上发生的方式,让我觉得我在布局中遗漏了一些东西。
注意:
如果我从app:layout_constraintBottom_toTopOf="@id/feedMessageInput" 到app:layout_constraintBottom_toBottomOf="parent" 删除RecyclerView 中EditText 的底部约束,则不会发生此问题,但是这是不可取的。
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 嗨,感谢您的评论。我可以确认这确实有效!如果你可以写作为答案(如果可能的话,你的理由为什么这样做)我会接受作为答案。我仍然不明白从
0dp到match_parent的宽度变化的要求,以及它如何影响高度,以及为什么这只需要反向布局。
标签: android xml android-recyclerview android-constraintlayout