Nicklas
Nicklas

Reputation: 435

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

I have a view pager which contains a fragment. When the fragment pager adapter first gets instantiated, the getItem(int position) method that must be implemented gets called twice, which is causing trouble in the Fragment since I am getting data based on the position. So for example, a LOG print reveals that it is first position 0, then position 1.

Any ideas why this is happening?

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

Upvotes: 4

Views: 3072

Answers (1)

NickL
NickL

Reputation: 4276

the getItem(int position) is called for loading (attaching) the fragment to its position in the fragment pager. By default, not only the visible fragment is loaded, but also the next one and previous one. So when you scroll to the 2nd 'page', you will see it will load the 3rd. When you go to the 3rd, it will unload the 1st fragment, and load the 4th. When you go back to the 2nd fragment, it wil load the 1st fragment again. And so on.

Upvotes: 6

Related Questions