Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

26. Loopers, Handlers, and HandlerThread


Now that you have downloaded and parsed JSON from Flickr, your next task is to download and
display images. In this chapter, you will learn how to use Looper, Handler, and HandlerThread to
dynamically download and display photos in PhotoGallery.


Preparing RecyclerView to Display Images


The current PhotoHolder in PhotoGalleryFragment simply provides TextViews for the
RecyclerView’s GridLayoutManager to display. Each TextView displays the caption of a GalleryItem.


To display photos, you are going to update PhotoHolder to provide ImageViews instead. Eventually,
each ImageView will display a photo downloaded from the mUrl of a GalleryItem.


Start by creating a new layout file for your gallery items called list_item_gallery.xml. This layout
will consist of a single ImageView (Figure 26.1).


Figure 26.1  Gallery item layout (res/layout/list_item_gallery.xml)


These ImageViews will be managed by RecyclerView’s GridLayoutManager, which means that
their width will vary. Their height, on the other hand, will remain fixed. To make the most of the
ImageView’s space, you have set its scaleType to centerCrop. This setting centers the image and then
scales it up so that the smaller dimension is equal to the view and the larger one is cropped on both
sides.

Free download pdf