Felipe A.
Felipe A.

Reputation: 949

Insert New Item's On RecyclerView

I have a REST Service that paginate my requests... I need to insert on my recyclerview 20 itens and keep the 20 itens before loaded.

This is my request...

VeiculoRequestHelper.veiculosPaginatedRequest(Request.Method.GET, url, null, new Response.Listener<VeiculoPaginated>() {
        @Override
        public void onResponse(VeiculoPaginated response) {
            AnalyticsTracker.getInstance().sendEvent(AnalyticsEnum.Category.MY_VEHICLE, AnalyticsEnum.Action.CLICK, AnalyticsEnum.Label.SUCCESS);

            isLast = response.isLast();

            ArrayList<Veiculo> veiculoArrayList = new ArrayList<>();
            veiculoArrayList.addAll(response.getContent());

            veiculos = veiculoArrayList;

            mAdapter.addItem(veiculos);

            mRecyclerView.setAdapter(mAdapter);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            AnalyticsTracker.getInstance().sendEvent(AnalyticsEnum.Category.MY_VEHICLE, AnalyticsEnum.Action.CLICK, AnalyticsEnum.Label.ERROR);

            Toast.makeText(getActivity(), "Erro ao realizar listagem de veículos.", Toast.LENGTH_LONG).show();
        }
    });

This is my adapter method to add new itens...

    public void addItem(ArrayList<Veiculo> veiculosArray) {
    if (veiculos != null) {
        veiculos.clear();
        veiculos.addAll(veiculosArray);
        notifyItemInserted(veiculos.size() - 1);
    } else {
        veiculos = veiculosArray;
    }
    notifyDataSetChanged();
}

What i'm doing wrong? The itens is inserted but not keeping the old itens... Help please!

Upvotes: 1

Views: 81

Answers (2)

Junaid Hafeez
Junaid Hafeez

Reputation: 1616

in your onResponse:

 ArrayList<Veiculo> veiculoArrayList = new ArrayList<>();
            veiculoArrayList.addAll(response.getContent());

            veiculos = veiculoArrayList;

            mAdapter.addItem(veiculos);

            mRecyclerView.setAdapter(mAdapter);

see what is happening, veiculoArrayList is being assigned to veiculos means new 20 items are now in it. passing veiculos into mAdapter.addItem(veiculos); and in your addItem method

veiculos.clear();
veiculos.addAll(veiculosArray);

means what ever was in veiculos will clear and new list veiculosArray would be added in it (means new 20 items). You just don't need to clear, add new items list and notifyDatasetChanged(). to keep focus on last item, you just need to set recyclerView.smoothScrollToPosition(position); and obviously position would be veiculos.size() before adding new items into it.

notifyItemRangeInserted(lastListSize, newList.size());

Upvotes: 0

beeb
beeb

Reputation: 1615

The old data will be cleared in addItem(ArrayList<Veiculo> veiculosArray):

if (veiculos != null) {
  veiculos.clear(); // here they will be cleared
  veiculos.addAll(veiculosArray);
  notifyItemInserted(veiculos.size() - 1);
}

Remove this line to keep the old items.

Upvotes: 1

Related Questions