I have a pager adapter that displays the content on my app. I wanted it to remember the place where it is at for when the orientation is changed. It works perfectly for all cases, except when I go to the last tab. It displays it fine, but when I change the orientation it goes back to the previous tab. It only happens on that one scenario. I added a log to see if it registers the change in position, it does it everywhere except on the last tab.
From onResume:
//A lot of code here
//if no book opened
if (s.getLibro() == null){
s.setLibro("Test");
}
if (s.getCap() == 0){
s.setCap(1);
}
//More code....
MyPagerAdapter adapter = new MyPagerAdapter();
pager = (ViewPager) findViewById(R.id.my_pager);
pager.setAdapter(adapter);
tabIndicator = (TabPageIndicator) findViewById(R.id.tab_indicator);
tabIndicator.setViewPager(pager);
pager.setCurrentItem(s.getCap()-1);
tabIndicator.setCurrentItem(s.getCap()-1);
Log.v(TAG, "FInished onResume");
The pager adapter:
private class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return numCap;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ListView list = (ListView) inflater.inflate(R.layout.capitulo, null);
//set the chapter you left off at, in case you change orientation
s.setCap(position);
//Load content
listll(position);
//Display it
list.setAdapter(new CapituloAdapter(getApplicationContext(), R.layout.content_main, ll));
((ViewPager) container).addView(list, 0);
//Only shows when position is any item from the first to the next to last, doesn't show for last item
Log.v(TAG, "Finished my pager adapter object" + (position-1));
return list;
}
//more code here...
The fact that the line Log.v(TAG, "Finished my pager adapter object" + (position-1)); doesn't appear when I enter the last tab worries me. Not sure what the issue is. When I navigate the app, it seems to be working fine (I can see the last tab without an issue).
The PagerAdapter
Pre-Loads the Next page. IE when you are viewing the 2nd page the PagerAdapter
loads the 3rd page. you are able to get the current page by subtracting 1 from the last instantiate page.
This breaks in the last page as the Adapter does not load the next page(as there is no next page).
The proper way to get the current position is to save the current position from ViewPager.OnPageChangeListener()
.