FRAGMENTS

In Chapter 2, you learn about the fragment feature that is new in Android 3.0. Using fragments, you can customize the user interface of your Android application by dynamically rearranging fragments to fit within an activity. This enables you to build applications that run on devices with different screen sizes.

As you have learned, fragments are really “mini-activities” that have their own life cycles. To create a fragment, you need a class that extends the Fragment base class. In Chapter 2, you learned how to create fragments and add them to your activities. Besides the Fragment base class, you can also extend from some other subclasses of the Fragment base class to create more specialized fragments. The following sections discuss the three subclasses of Fragment: ListFragment, DialogFragment, and PreferenceFragment.

ListFragment

A list fragment is a fragment that contains a ListView, displaying a list of items from a data source such as an array or a Cursor. A list fragment is very useful, as you may often have one fragment that contains a list of items (such as a list of RSS postings), and another fragment that displays the details of the selected posting. To create a list fragment, you need to extend the ListFragment base class.

The following Try It Out shows you how to get started with a list fragment.

TRY IT OUT: Creating and Using a List Fragment

codefile ListFragmentExample.zip available for download at Wrox.com

1. Using Eclipse, create an Android 3.0 project ...

Get Beginning Android now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.