This is a sample from the book.

Sample 1 Sample 2 Sample 3 Sample 4 Sample 5


GridViewPager CardFragments

Using the GridViewPager with CardFragments

As described in the previous exercise, there are two ways to implement a GridViewPager:

  • Assigning a fragment such as a CardFragment for each page by extending FragmentGridPagerAdapter.
  • Without fragment by extending GridPagerAdapter and by providing a custom View object. (Click here to view this exercise)

Implementing the GridViewPager with CardFragments allows users to navigate and choose from a set of items shown as cards. Cards are one of the most important UI patterns in Android Wear. They offer a consistent look and feel and reflect a design pattern that users are already accustomed to. In addition, cards provide you with a number of useful features such as scrollable content support, card gravity, and card expansion. Finally, if the standard layout doesn’t suit your needs, you can always create a card with a custom layout.

In the following Step-by-Step section, you will learn how to create a simple 3 by 3 grid of items using the GridViewPager with CardFragments as shown in the figure above.

Let’s get started!

1. Using Android Studio, create a new Android project for the Wear platform and name it GridViewFragments.

2. Add a new file in the res/layout folder and name it activity_main. Key in Framelayout in the Root Tag field. Populate it as follows:

3. Add a new XML file in the res/layout folder and name it activity_workout. Key in LinearLayout in the Root Tag field. Populate it as follows:

4. Add a new Class file and name it GridViewFragmentActivity. Populate it with the following content:

5. In the AndroidManifest.xml, add the following content enclosed in the <activity></activity> XML nodes:

8. That’s it! Now press Shift+F9 to debug the project on a wearable device or emulator.

Alex Ho • Email Me ©2015

Go to next sample


Enjoyed the samples?


Signup to receive additional content for free.

(Don't forget to check your email and confirm your subscription)

We won't send you spam. Unsubscribe at any time.