In Listing 11.8 title does not shows OK, p.207


Hi !
The first item opened in my CrimePagerActivity doesn’t show correctly the title (it shows it ok after swiping it). It must be set on CrimePagerActivity - onCreate also. With the following function for example, called from CrimePagerActivity, onCreate, after setting the current item, and from onPageSelected in mViewPager.setOnPageChangeListener, all is OK:

	private void setFragmentTitle(int i) {
		Crime c = mCrimes.get(i);
		if (c.getTitle() != null) {

Best regards


Good catch.

This seems to only be an issue when selecting the first item in the list. The OnPageChangeListener.onPageSelected() method does not called after ViewPager.setCurrentItem(0) is called.

Another idea with your setFragmentTitle() method might be to only call it when first item is was selected.

UUID crimeId = (UUID) getIntent().getSerializableExtra( CrimeFragment.EXTRA_CRIME_ID); for (int i = 0; i < mCrimes.size(); i++) { if (mCrimes.get(i).getId().equals(crimeId)) { mViewPager.setCurrentItem(i); if (i == 0) { // work around to set title for first item in list setFragmentTitle(i); } break; } }


I’m finding the same problem but it happens regardless of which list item I click - not just the first. Which makes sense because we are setting the title in a page change listener, which would have no reason to fire until we move from one page to another.


I had the same problem but the suggested code did not work for me. Couldnt resolve setFragmentTitle. Perhaps that’s in android fragment and not in support lib fragment. In any event I found that this works:

	for (int i=0; i <mCrimes.size(); i++) {             // linear search
		if (mCrimes.get(i).getId().equals(crimeId)) {  // search for the correct crime
		if (i == 0) setTitle(mCrimes.get(i).getTitle()); // work around to set title for first item in list	                         

Although I dont understand why that’s needed. It’s as if setTitle() is not being invoked for 0th position. But in CrimeLab setTitle is being called for each of the 100 elements. We know this is true because the title TextView shows it.


There are two null methods, onPageScrollStateChanged and onPageScrolled, in Listing 11.8. I call them null because they do nothing. I assume they’re there because the parent class method’s do something that we don’t wish to have done. If that’s true, then shouldn’t there be an “@Override”?



You have to implement those methods because they are defined as abstract by ViewPager.OnPageChangeListener. Of course they don’t have to do anything. You can add @Override if you want. I did.