I have a method that returns a certain number of records and their total number in json, the answer looks like this:

{ hits:100 ...тут идут переданные записи } 

In the query parameters, you can specify two parameters limit - the number of records to be transferred and page shift on records to the limit. There can be about 1000 records and it is obviously impossible to load them all at once. I did some semblance of automatic upload, it works like this:

  1. The first query is made with page 0 and limit 50
  2. After parsing the answer, look at the hits and make the request again.
  3. Repeat until goal is reached

I do not like a few moments

  1. All items are loaded immediately.
  2. When updating the list, RecyclerView jumps to the first item.

What is the best way to deal with these two problems?

    1 answer 1

      // 1. У вас на экране не все, только то что видите, все остальные элементы дестроятся, как только "ушли" за пределы экрана. // Для сохранения текущей позиции запомните высоту списка до //обновления, к примеру через `OnScrollListener`: // private int mScrollY; // private RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() { // @Override // public void onScrolled(RecyclerView recyclerView, int dx, int dy) { // super.onScrolled(recyclerView, dx, dy); // mScrollY += dy; // } // }; // После обновления данных вызывайте программно скролл // mRecyclerView.scrollBy(0, mStateScrollY); 

    UPDATE
    Over time, a more correct solution was read. Add a method to your adapter for RecyclerView

     public void setData(List<String> data) { mStringData.addAll(data); // ваша коллекция данных для адаптера notifyItemInserted(getItemCount() - data.size()); } 

    As a result, without bicycles, the adapter will add more elements to the end of the list, without any jerking or memorization of the scrolling position on the screen.