The ViewPager has 5 pages (which I want to reuse then), the corresponding PagerAdapter has an array with views, I fill it at the start of the program and then show the central page:

0 1 |2| 3 4 

Now, when paging, I use the page from the opposite end of the array, updating it with new data (based on calendar dates), depending on the paging direction:

 повторное использование: 4 0 |1| 2 3 или 1 2 |3| 4 0 и тд 

I onPageSelected pages in onPageSelected from ViewPager.OnPageChangeListener , but at the same time when scrolling in the animation there is a delay due to the rather slow code when updating the page (the code is not accelerated).

Therefore, I rendered the slow code into a separate thread and then updated the pages using the post view method. Work has become faster, but still slows down.

The question is how can you optimize / rewrite such an algorithm?

Update

The question is this. As I wrote, the execution of the code was carried out into the stream, but it still slows down due to the large number of views on the page, apparently due to the post call, I tried to do everything in the stream from beginning to end - from inflite to filling up the views, but the problem occurred trying to slip a page created like this in a PagerAdapter and then call notifyDataSetChanged() - after that crashes

 android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 

Maybe this is due to the fact that I, as a rootView transfer the rootView created in the main thread?

 getLayoutInflater().inflate(R.layout.layout_calendar, mViewPager, false); 

Update 2

It seems to have figured out with this, I will reply

  • Load 9 pages, for example. 5 displayed, 2 on the right and left. When reaching page 1 or 5, start loading 2 pages to the right or left. The loaded pages are not immediately cleaned: leave two sets of 5 pages (i.e. up to 10 pages hang in the memory. The oldest by date are deleted, the date is updated by page access time). - DimXenon
  • Displays only one of the five, central. - schmidt9
  • 1 is displayed, on the right and on the left, by 2, in addition, to fill the buffer from the list (those that are on 2 on the right and on the left) - another 2. To have a supply of pages for display and a reserve of pages for replenishing the "display queue" - those pages to the right and left of the center element. If an extreme element is chosen at once, we already have two ready to fill in the future two extreme ones. And we will ship the next two in reserve. At the same time, we make sure that the place (resources) becomes free from the opposite side. - DimXenon
  • one
    @DimXenon, updated the question - schmidt9
  • We're waiting for an answer!) - whalemare

1 answer 1

For lack of sufficient time for clarification I will leave here a link to the copy-paste of the finished code from the project, yet it is better than nothing. Who wants to discuss / bring to mind in terms of portability please ask