使Recycler View显示从底部开始的行

Gud*_*din 31 android

我看到了从底部到顶部RecyclerView显示的某种方法ViewHolders.现在,我找不到它的任何地方(后经历时间的一半RecyclerView,RecyclerAdapter,LayoutManager...).

And*_*dEx 78

难道LinearLayoutManager.setStackFromEnd(true)你正在寻找?

编辑

事实证明了这LinearLayoutManager.setReverseLayout(true)一点.无论哪种方式,读者可能想要尝试每种方法和两者的组合以获得所需的效果.

  • 我不敢相信应该很简单的与 Android 相关的东西实际上很简单。已经习惯了谷歌搜索,“如何在 android 中做[简单的任务]?”,并且看到最流行的答案是“用一只脚做平衡动作,玩弄 3 个桃子和 8 个椰子,然后跳跃一边哼着‘江南Style’的节拍,一边通过9圈。” (3认同)
  • 好吧,我有时间来测试它:.setStackFromEnd(true)不起作用,但.setReverseLayout(true)确实完美.无论如何,我会接受你的答案是正确的(你可能想在未来的观众中添加.setReverseLayout). (2认同)

Abh*_*yMe 8

这是科特林的解决方案

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
Run Code Online (Sandbox Code Playgroud)

或者,如果您喜欢apply方法:

recycler.apply { 
    layoutManager = LinearLayoutManager(this).apply { 
        stackFromEnd = true
        reverseLayout = true
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 一个说反向布局为假,另一个说反向布局为真。 (2认同)

Mos*_*ter 6

您可以通过在xml代码中添加两行来完成此任务。

app:stackFromEnd="true"
app:reverseLayout="true"
Run Code Online (Sandbox Code Playgroud)

这将与所有聊天应用程序一起使用。

  • 是的,当然,如果您从服务中动态加载项目,则在添加任何项目后应通过添加以下行“ mRecyclerView.scrollToPosition(mAdapter.getItemCount()-1);”滚动到底部。 (3认同)
  • 似乎不起作用,是否需要其他更改? (2认同)

Ram*_*bry 5

如果您使用的是LinearLayoutManager,请将第三个参数(reverseLayout)设为 false

LinearLayoutManager linearLayoutManager =
            new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
Run Code Online (Sandbox Code Playgroud)