Chapter 25 HTTP and Background Tasks
In PhotoGalleryActivity.java, set up PhotoGalleryActivity as a SingleFragmentActivity
by deleting the code that the template generated and replacing it with an implementation of
createFragment(). Have createFragment() return an instance of PhotoGalleryFragment.
(Bear with the error that this code will cause for the moment. It will go away after you create the
PhotoGalleryFragment class.)
Listing 25.1 Activity setup (PhotoGalleryActivity.java)
public class PhotoGalleryActivity extends Activity SingleFragmentActivity {
@Override
protected Fragment createFragment() {
return PhotoGalleryFragment.newInstance();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
/ Auto-generated template code... /
}
}
PhotoGallery will display its results in a RecyclerView, using the built-in GridLayoutManager to
arrange the items in a grid.
First, add the RecyclerView library as a dependency, as you did in Chapter 8. Open the project
structure window and select the app module on the left. Select the Dependencies tab and click the
- button. Select Library dependency from the drop-down menu that appears. Find and select the
recyclerview-v7 library and click OK.
Rename layout/activity_photo_gallery.xml to layout/fragment_photo_gallery.xml to create a
layout for the fragment. Then replace its contents with the RecyclerView shown in Figure 25.4.
Figure 25.4 A RecyclerView (layout/fragment_photo_gallery.xml)