Android.support.v4.app.FragmentPagerAdapter getItem (int position) called twice?

I have a view pager that contains a snippet. When an instance of the fragment pager adapter is first created, the getItem (int position) method, which must be implemented, is called twice, which causes problems in the Fragments, since I get data based on the position. So, for example, a LOG printout shows that this is the first position 0, then position 1.

Any ideas why this is happening?

I follow the example here: http://wptrafficanalyzer.in/blog/implementing-horizontal-view-swiping-using-viewpager-and-fragmentpageradapter-in-android/

+4
source share
1 answer

getItem(int position) is called to load (attach) a fragment to its position in the fragment pager. By default, not only the visible fragment is loaded, but also the next and previous. Therefore, when you go to the second page, you will see that it loads the third. When you go to the 3rd, it will unload the 1st fragment and load the 4th. When you return to the second fragment, load the first fragment again. And so on.

+6
source

Source: https://habr.com/ru/post/1442982/


All Articles