gpt4 book ai didi

android - 使用 stackFromEnd = true 将项目加载到 RecyclerView 中不会滚动到底部

转载 作者:行者123 更新时间:2023-12-05 00:05:46 28 4
gpt4 key购买 nike

我有一个聊天应用程序,我试图确保当用户打开屏幕时,项目从底部显示,最近的项目位于输入区域的正上方。我为输入区域使用了自定义 View ,回收器和自定义 View 都在 ConstraintLayout 中。

我的问题是,当我将项目加载到列表中时,如果项目的数量大于回收器的大小,它将不会完全显示最后一个项目。当我使输入区域 visibility = Gone 时,项目会在底部正确显示。这几乎就像回收器 LinearLayoutManager 认为回收器的高度是没有输入字段的屏幕。我已经手动打印出 View 的大小并使用布局检查器来确保回收器确实绘制在正确的位置(在输入上方和导航栏下方)。

什么可能导致这样的问题?我应该注意到,每当您单击聊天气泡中的链接文本时,列表都会滚动少量,等于您打开屏幕时不正确的偏移量。显然,这里没有衡量标准,也不确定从哪里开始。

我还应该注意,如果我尝试使用 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)
}
}

还有我的 fragment 的 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>

感谢任何帮助,我迷路了..

最佳答案

对我来说,这更像是布局中的问题,而不是滚动功能中的问题。如果您使用相对布局而不是线性布局,我希望这个问题可以轻松解决。因此,如果它可能有帮助,我将使用相对布局在下面添加我的解决方案。

<RelativeLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/input_field">
</androidx.recyclerview.widget.RecyclerView>

<EditText
android:id="@+id/input_field"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>

因此,如果不清楚我做了什么,一个 EditText 元素被设置为与父元素的底部对齐,这是一个相对布局。然后添加一个 RecyclerView,这样 RecylerView 将始终约束在 EditText 之上,但不会重叠。

关于android - 使用 stackFromEnd = true 将项目加载到 RecyclerView 中不会滚动到底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57438713/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com