簡體   English   中英

在頂部將新項目添加到recyclerview

[英]Add new item to recyclerview at the top

當我通過以下方式將新商品添加到我的recyclerview中時:

dbHelper.insertAusgabe(tag,datum,ausgabe,menge,kategorie);
Model model = new Model(tag,datum,kategorie,ausgabe,menge);
rvList.add(model);
modelAdapter.notifyItemInserted(rvList.size()-1);

Recyclerview:

modelAdapter = new ModelAdapter(rvList,ScrollingActivity.this);

RecyclerView.LayoutManager layoutManager = new 
LinearLayoutManager(ScrollingActivity.this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setFocusable(false);
recyclerView.requestFocus();

該項目已成功添加,但位於recyclerview的底部,而不是應有的頂部。 在此處輸入圖片說明

那是因為您也將其添加到列表的底部。

rvList.add(model); 應該是rvList.add(0, model);

並通知您的recyclerview不是這樣

modelAdapter.notifyItemInserted(rvList.size()-1); 但是像這個modelAdapter.notifyItemInserted(0);

在您的列表中嘗試使用此代碼。

            Collections.reverse(rvList);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM