Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion on 'position' in instantiateItem function in class PagerAdapter (android)

I'm currently going through a Horizontal View Paging tutorial: http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/

For testing purposes, I added the following line as the first line in the instantiateItem(...) function:

Log.d("SimpleViewPagerActivity", "Position " + position);

(The following is confusing so it would be better if you try out the example yourself to see what I'm talking about it in the following paragraph): In this example, there are 5 ViewPager positions (which are zero-based) and the Activity is set to start on position 2. When I start the Activity, the Log says that the initial position goes from 1 then 2 and then 3. If I swipe to the left, the log says the position is 0. When I swipe to the left again, there is no log message. From here, I swipe to the right and now the position is 2. I swipe to the right again and the position is 3. I swipe to the other positions on the right and I get results that I cannot explain.

Can someone explain the 'position' variable in instatiateItem() or point me to somewhere where it's explained? I am currently on the Android Compatibility package v.6

like image 990
tehawtness Avatar asked Jan 17 '23 12:01

tehawtness


1 Answers

Inside of instantiateItem, the position parameter is the position that is in need of rendering. It is NOT the position of the currently focused item that the user would see. The pages to the left and right of the currently displayed view need to be pre rendered in memory so that the animations to those screens will be smooth.

like image 115
Paul Nikonowicz Avatar answered Jan 30 '23 14:01

Paul Nikonowicz