【发布时间】:2019-08-10 01:52:38
【问题描述】:
我有一个聊天应用程序,我试图确保当用户打开屏幕时,项目从底部显示,最新的项目位于输入区域的正上方。我为输入区域使用了自定义视图,并且回收器和自定义视图都在 ConstraintLayout 中。
我的问题是,当我将项目加载到列表中时,如果项目的数量大于回收器的大小,它不会完全显示最后一个项目。当我使输入区域可见性 = Gone 时,项目会在底部正确显示。这几乎就像回收器 LinearLayoutManager 认为回收器的高度是没有输入字段的屏幕的高度。我已经手动打印出视图的大小并使用布局检查器来确保回收器确实绘制在正确的位置(输入上方和导航栏下方)。
什么可能导致这样的问题?我应该注意,每当您单击聊天气泡中的链接文本时,列表会滚动少量等于打开屏幕时不正确的偏移量。很明显,这里有些东西无法衡量,也不知道从哪里开始。
我还应该注意,如果我尝试使用 smoothScroll 添加帖子,它将转到列表的末尾,但是每当发送消息时列表中出现新项目时,最近添加的项目上方的项目似乎用不必要的动画跳起来一点。好像列表中的最后一项处于某种特殊状态?
如果你好奇,这是我的滚动功能:
private fun scrollToFirstItem(dueToKeyboard: Boolean = false) {
val stackingFromEnd = (recyclerView.layoutManager as LinearLayoutManager).stackFromEnd
if (stackingFromEnd) {
val firstPosition = recyclerView.adapter?.itemCount?: 0 - 1
if (dueToKeyboard ) {
recyclerView.scrollBy(0, Integer.MAX_VALUE)
} else {
recyclerView.scrollToPosition(firstPosition)
}
recyclerView.post { recyclerView.smoothScrollToPosition(firstPosition) }
} else {
recyclerView.scrollToPosition(0)
}
}
还有我的片段的 xml:
<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">
<include
android:id="@+id/searchView"
layout="@layout/compose_new_message_include"/>
<com.airbnb.epoxy.EpoxyRecyclerView
android:id="@+id/conversationEpoxyRV"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scrollbars="vertical"
app:layout_constraintTop_toBottomOf="@id/searchView"
app:layout_constraintBottom_toTopOf="@id/composeView"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
tools:listitem="@layout/conversation_item_inbound"/>
<include layout="@layout/conversation_pip_view"
android:id="@+id/selectedMediaContainer"/>
<****.ComposeView
android:id="@+id/composeView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
感谢任何帮助,我很迷茫..
【问题讨论】:
-
提供你的xml文件代码
-
我也实现了。我只是对你说的话感到困惑。因此,当您发送消息时,您是将该消息附加到同一个列表中还是完全显示为不同的项目?
-
附加到列表中
-
实际上最终修复它的是 reverselayout 和 stackfromend 都必须是真的......这有点与文档相反,很明显是一个 android 错误
标签: android android-recyclerview