我有一个 RecyclerView 定义为:
<android.support.v7.widget.RecyclerView
android:id="@+id/message_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/message_input"
android:layout_alignParentTop="true"
app:stackFromEnd="true" />
相关代码也是常用的:
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//layoutManager.setStackFromEnd(true);
mRecyclerView.setLayoutManager(layoutManager);
但是,当我添加一个项目 RecyclerView
时,它不遵守 app:stackFromEnd="true"
。另一方面,如果我取消注释并以编程方式使用 layoutManager.setStackFromEnd(true);
,它工作正常。我缺少什么问题?欢迎任何想法。
最佳答案
我遇到了类似的事情。问题在于,当 RecyclerView 膨胀时,它会读取属性。
但是您要分配一个在 java 代码中创建的新 LinearLayoutManager。这个新管理器没有将 stackFromEnd 属性设置为 true(默认为 false)。
stackFromEnd
是管理器的属性,而不是 RecyclerView
。
在我的例子中,我有这个:
<android.support.v7.widget.RecyclerView
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:id="@+id/list"
app:stackFromEnd = "true"
app:layoutManager="LinearLayoutManager"
</android.support.v7.widget.RecyclerView>
在您的 xml 代码中,您没有 app:layoutManager
属性,因此我不确定在通货膨胀时为您创建了哪个管理器。
关于android - 应用程序 :stackFromEnd for RecyclerView is not working in xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34286876/