Join David Gassner for an in-depth discussion in this video Using a List fragment, part of Building Adaptive Android Apps with Fragments.
- Android apps that display lists in activities typically use a class named "List Activity," that handles the list presentation and a lot of the interaction functionality. In a fragments-based app, you can use a class named "List Fragment" that does much the same thing. In this version of my project, I've added a couple of new assets. First, I've added a class named "Flower Array Adapter." This is an extension of the array adapter class and it uses a class named "Flower" that I've also added.
This class has a number of private fields, and then getters and setters, to represent a single instance of a data object named "Flower." Down at the bottom, there's a constructor method, and then a couple of methods to transform the flower object to and from a bundle. I'll be using those methods later in the course. The goal of the Flower Array Adapter class is to take an array list of flower objects and then display each flower using a layout.
The layout has also been provided in this project; it's called "Flower List Item" and it has an image view and a text view to display the image and the name of the flower. And finally, in the Resources folder under "Drawable XHDPI," I've also added a number of images. And then under Java, under the Data package, there's a Flower Data class, which adds a few items to an array list. So all these elements together will let me add data to the app.
And now my goal is to take my fragment that's being used for the main screen, and turn it into a list that presents this data. So I'll close all of these editor windows and I'll go back to my project view, and I'll start in the Layout file for my fragment. In the Layout folder, that's myfragment.xml. Right now, this fragment contains just a text view object. I'm going to remove that text view object and replace it with a list view object.
I'll set the required layout width to "Match Parent" and the height to "Wrap Content." And then I'll add the required ID. When you use the List Fragment or List Activity classes, the XML Layout file must have this list view widget and the list view has a required ID. It's named "List," but it comes from the Android framework. So the syntax is "@android:id/list." So that's the change you need to make to the fragment layout.
Now let's go to the fragment class. I'll go to my main Java package and open "My Fragment." First, I'm going to change the super class of this class from "Fragment" to "List Fragment." The List Fragment class is a member of android.app. Now notice, I've already declared a list of flower objects, and I'm going to get that data by instantiating the Flower Data class that I showed you earlier with New Flower Data, and then, from that object, I'll call it "Get Data Method." Now as the class is instantiated, I'll be retrieving the data, and I'll have a list of data objects to work with.
In a real production application, you could replace this with code that gets the data from a local database, or perhaps from a web service. The next step is to fill in this oncreate method that I've already added. I'll expand the code a little bit, and then add code after the call to super.oncreate. I'll create an instance of my Flower Array Adapter class that I've added to this project and I'll name it "Adapter," and I'll instantiate it with the constructor method "New Flower Array Adapter." The method expects a context, and I'm going to pass in the current activity for the current fragment, by calling the "Get Activity Method." Next, I need a resource.
This will be the ID of the layout that's used to display each item in the list. And as I showed previously, I have a new XML Layout file that I've added to the project, and it's ID is "r.layout.flowerlistitem." And finally, the third argument is the data. And I've already created that; it's named "Flowers." So now I've created the adapter object, I've passed it a reference to the current activity, I've passed it the XML Layout file I want to use for each item in the list, and I've passed it the data.
Now I'm ready to set the adapter. And just like with the List Activity, a list fragment has the method, "Set List Adapter." I'll pass in the adapter object, and now, as the fragment comes to the screen, it'll go get the data and display it in a list. I'll build my project and make sure that I don't have any errors and then I'll try running it in my emulator. And there's the result: the flower data being displayed in a list. And this list is now a part of the fragment.
When I run the app on a cell phone, the list will take up the entire screen. But later on, when I adapt this application so it can also run on tablets or other larger devices, this list will only fill up the left side of the screen.
- Understanding fragments
- Adding fragments to activities
- Creating layouts for multiple screens
- Understanding arguments and callback methods
- Updating apps with support for fragments