我正在创建一个聊天屏幕。因此,我必须使用从下到上的消息列表,这是一个问题:底部元素没有完全显示,仅显示了一半。
这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<com.globus_ltd.elife.features.common.ProgressView
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvMessages"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/divider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/item_message" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:visibility="gone"
android:id="@+id/fabScrollDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:src="@drawable/ic_fab_down_24dp"
android:tint="@color/white"
app:fabSize="mini"
app:layout_constraintBottom_toBottomOf="@+id/rvMessages"
app:layout_constraintEnd_toEndOf="parent" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="240dp"
android:orientation="horizontal"
android:paddingStart="@dimen/dimen_8dp"
android:paddingEnd="@dimen/dimen_8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<EditText
android:id="@+id/etInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="@string/chat_hint"
android:inputType="textMultiLine"
android:maxHeight="180dp" />
<ImageButton
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="0"
android:background="@android:color/transparent"
android:padding="@dimen/dimen_12dp"
android:tint="@color/accent"
app:srcCompat="@drawable/ic_send_black_24dp" />
</LinearLayout>
<View
android:id="@+id/divider"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@color/divider"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的代码:
rvMessages.apply {
val lm = LinearLayoutManager(context)
lm.reverseLayout = true
layoutManager = lm
}
我找到了解决方法
但我认为这是拐杖。
我使用了OnChildAttachStateChangeListener,它在添加第一个元素时调用scrollToPosition
rvMessages.addOnChildAttachStateChangeListener(object : RecyclerView.OnChildAttachStateChangeListener {
override fun onChildViewDetachedFromWindow(view: View) {
}
override fun onChildViewAttachedToWindow(view: View) {
// lm is my LinearLayoutManager
if (lm.childCount == 1) {
smoothScrollToPosition(0)
}
}
那么,有正确的解决方案吗?