Add new item to recyclerview at the top

时间:2019-04-17 01:34:50

标签: android android-recyclerview

When I add a new item to my recyclerview with:

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();

The item gets successfully added but at the bottom of the recyclerview and not at the top as it should. enter image description here

2 个答案:

答案 0 :(得分:1)

that's because you added it at the bottom of your list too..

rvList.add(model); should be rvList.add(0, model);

and notify your recyclerview not like this

modelAdapter.notifyItemInserted(rvList.size()-1); but like this modelAdapter.notifyItemInserted(0);

答案 1 :(得分:0)

Try this code with your list.

            Collections.reverse(rvList);