0

通常,当我们向 recylerview 添加新项目时,新项目会出现在前一个项目的下方。我需要将每个新项目都放在列表的顶部。我们怎么能这样做。谢谢

4

2 回答 2

0

在 XML 文件中尝试以下更改。

 <androidx.recyclerview.widget.RecyclerView
  android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:clipToPadding="false"
    android:scrollbars="none"
    app:layoutManager="LinearLayoutManager"
    app:stackFromEnd="true"
    app:reverseLayout="true/>
于 2021-04-28T09:54:27.547 回答
0

要将项目添加到列表视图的顶部而不是将其添加到底部,您必须将其设为 (datasource,array,list ...) 的第一个元素,这是一种使其成为 {

List items = new ArrayList();

//some fictitious objectList where we're populating data
for(Object obj : objectList) {
    items.add(0, obj);
    listAdapter.notifyDataSetChanged();
}

listView.post(new Runnable() {
    @Override
    public void run() {
        listView.smoothScrollToPosition(0);
    }
}

}

于 2021-04-28T09:55:39.513 回答