How to Implement Android Horizontal Paging, Page 2

A Quick Android Horizontal Paging Example

Let's show a quick example of how to create a three-page ViewPager control. To do so, follow these steps:

Create a new Android project in Eclipse, or choose to modify an existing one.

Add the Android Compatibility package to the project as described above.

Create three layout resource files, one for each page in your ViewPager control. For example, for the sample application we created three layouts, each with a TextView and an ImageView (as shown in Figure 3).

Update the main.xmllayout file and include a ViewPager control. This control must be defined using its fully qualified control name. Here's an example:

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/threepageviewer"/>

Update the Activity class and implement a custom PagerAdapter class. You will need to implement several important data adapter methods for this class. The getCount() method must return the number of pages for the ViewPager (in this case, three). The instantiateItem() method must inflate the appropriate view control for each page (in our example, a full layout), populate it with any data, as determined by the page position, and add it to the collection used by the ViewPager control. With a three page layout, we have defined the left layout as position 0, the middle layout as position 1, and the right layout as position 2.

Finally, you'll need to implement the destroyItem() method to remove each page's contents when it is no longer being displayed.

You need to update the onCreate() method of your Activity class to bind your custom PagerAdapter to the ViewPager control you defined in your Activity layout resource file, main.xml. You set the data adapter using the setAdapter() method. You can also modify the default starting position of the ViewPager, so that the Monkey (position 1, the middle page) is the default using the setCurrentItem() method.

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

MyPagerAdapter adapter = new MyPagerAdapter();

ViewPager myPager = (ViewPager) findViewById(R.id.threepageviewer);

myPager.setAdapter(adapter);

myPager.setCurrentItem(1);

}

That's all there is to it. Now you've got a three-page horizontal paging screen, as shown in Figure 4, that loads the middle page by default (the monkey). The user can swipe or page to the left to see the lion page or right to see the rhino page.

What About Fragments?

Got an application that relies upon fragments? No problem! There's nothing stopping you from using fragments as your ViewPager control page contents. Take a look at the FragmentPagerAdapter for this very purpose and serve up fragment-based pages.

Conclusion

Android users desire applications that have easy-to-navigate user interfaces. The horizontal paging functionality, available within the Android Compatibility package, gives application developers yet another handy technique for displaying substantial quantities of information in such a fashion. Whether your application is fragment-ready or not, you can use the ViewPager and related data adapter classes available to create dynamic user interfaces for your users.