Android 如何在recyclerView中将新条目添加到列表顶部

Android 如何在recyclerView中将新条目添加到列表顶部,android,android-recyclerview,Android,Android Recyclerview,通常,当我们向recylerview添加新项目时,新项目刚好位于前一个项目的下方。我需要把每一个新项目都列在清单的首位。我们怎么能这样做呢。谢谢在XML文件中尝试以下更改 <androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:orient

通常,当我们向recylerview添加新项目时,新项目刚好位于前一个项目的下方。我需要把每一个新项目都列在清单的首位。我们怎么能这样做呢。谢谢

在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/>

若要将项目添加到listview的顶部,而不是添加到底部,您必须将其作为(数据源、数组、列表…)的第一个元素
这里有一个办法
{

}

试试这个
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);
    }
}