我曾经在某处看到过一种方法,可以使 RecyclerView
从底部向顶部显示 ViewHolders
。现在,我已经找不到它了(在查阅了半个小时的 RecyclerView
、RecyclerAdapter
、LayoutManager
等内容后)。
我曾经在某处看到过一种方法,可以使 RecyclerView
从底部向顶部显示 ViewHolders
。现在,我已经找不到它了(在查阅了半个小时的 RecyclerView
、RecyclerAdapter
、LayoutManager
等内容后)。
您是否正在寻找LinearLayoutManager.setStackFromEnd(true)
方法?
编辑
事实证明,LinearLayoutManager.setReverseLayout(true)
方法也可以完成任务。无论如何,读者可能需要尝试这两种方法的单独使用以及组合使用,以获得所需的效果。
LinearLayoutManager
构造函数的最后一个参数是reverseLayout
,当设置为true时,似乎会影响我设置的ItemDecorator
。如果将此参数设置为false,则ItemDecorator
会按预期布置项目。 - Etienne Lawlor这是用Kotlin编写的解决方案。
val llm = LinearLayoutManager(this)
llm.stackFromEnd = true // items gravity sticks to bottom
llm.reverseLayout = false // item list sorting (new messages start from the bottom)
rv_chat_history.layoutManager = llm
或者,如果您喜欢apply方法:
recycler.apply {
layoutManager = LinearLayoutManager(this).apply {
stackFromEnd = true
reverseLayout = false
}
}
您可以通过在xml代码中添加两行来完成此任务。
app:stackFromEnd="false"
app:reverseLayout="true"
这将像所有的聊天应用一样工作。
mRecyclerView.scrollToPosition(mAdapter.getItemCount()-1);
- Mostafa Anter如果您使用的是LinearLayoutManager,请将第三个参数(reverseLayout)设置为false
LinearLayoutManager linearLayoutManager =
new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
stackFromEnd = true
,reverseLayout = false
并设置scrollToPosition(index_of_last_element)
来实现的。recyclerView?.apply {
layoutManager = LinearLayoutManager(context).apply {
stackFromEnd = true
reverseLayout = false
}
}
recyclerView?.scrollToPosition(equations.size - 1)
reverseLayout = true
。 - Kidus TekestereverseLayout = true
。然而在我的情况下,将其设置为false就有效了。您可以在提供的动画中看到它正在工作。 - xFactor关于您的查询的解决方案:
LinearLayoutManager layoutManager=
new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
val latestMessagePosition = liveMessagesData.size - 1
messagesAdaptor.setData(liveMessagesData)
if (latestMessagePosition >= 0) {
recyclerViewMessages.scrollToPosition(latestMessagePosition)
}
This is working for me in kotlin
recyclerView?.scrollToPosition(mArrayList.size - 1)