The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


More Fun with Pagers

In earlier chapters, we saw basic uses of ViewPager, along with ways to show multiple pages at a time on larger screens. However, there are other ways to apply ViewPager and integrate it into the rest of your application, some of which we will examine in this chapter.

Prerequisites

This chapter assumes that you have read the core chapters, particularly the one showing how to use ViewPager.

Hosting ViewPager in a Fragment

Classically, the primary restriction on ViewPager was that you could not both have ViewPager be in a fragment and have ViewPager host fragments as its pages. You could do one or the other, but not both simultaneously.

As noted in a previous chapter, Android 4.2+ supports nested fragments natively, and the latest Android Support package backport also supports nested fragments. With those, you can have ViewPager be in a fragment and host fragments as its pages. However, it requires a minor modification to the way we set up a PagerAdapter, as is illustrated in the ViewPager/Nested sample project. This is the same project as ViewPager/Indicator, with the twist that the pages are fragments and the ViewPager is inside a fragment.

Our activity now implements the standard add-the-fragment-if-it-does-not-exist pattern that we have seen previously:

package com.commonsware.android.pagernested;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class ViewPagerIndicatorActivity extends FragmentActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
      getSupportFragmentManager().beginTransaction()
                                 .add(android.R.id.content,
                                      new PagerFragment()).commit();
    }
  }
}

This loads a PagerFragment, which contains most of the logic from our original activity:

package com.commonsware.android.pagernested;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PagerFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater,
                           ViewGroup container,
                           Bundle savedInstanceState) {
    View result=inflater.inflate(R.layout.pager, container, false);
    ViewPager pager=(ViewPager)result.findViewById(R.id.pager);

    pager.setAdapter(buildAdapter());

    return(result);
  }

  private PagerAdapter buildAdapter() {
    return(new SampleAdapter(getActivity(), getChildFragmentManager()));
  }
}

The biggest difference is that our call to the constructor of SampleAdapter no longer uses getSupportFragmentManager(). Instead, it uses getChildFragmentManager(). This allows SampleAdapter to use fragments hosted by PagerFragment, rather than ones hosted by the activity as a whole.

No other code changes are required, and from the user’s standpoint, there is no visible difference.

Pages and the Action Bar

The preview of this section apparently resembled a Pokémon.

ViewPagers and Scrollable Contents

The preview of this section was whisked away by a shark-infested tornado.

Columns for Large, Pages for Small

The preview of this section is in the process of being translated from its native Klingon.