1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.gridviewpager; 18 19 import android.app.Fragment; 20 import android.app.FragmentManager; 21 import android.content.Context; 22 import android.support.wearable.view.CardFragment; 23 import android.support.wearable.view.FragmentGridPagerAdapter; 24 import android.support.wearable.view.ImageReference; 25 import android.view.Gravity; 26 27 /** 28 * Constructs fragments as requested by the GridViewPager. For each row a 29 * different background is provided. 30 */ 31 public class SampleGridPagerAdapter extends FragmentGridPagerAdapter { 32 33 private final Context mContext; 34 SampleGridPagerAdapter(Context ctx, FragmentManager fm)35 public SampleGridPagerAdapter(Context ctx, FragmentManager fm) { 36 super(fm); 37 mContext = ctx; 38 } 39 40 static final int[] BG_IMAGES = new int[] { 41 R.drawable.debug_background_1, 42 R.drawable.debug_background_2, 43 R.drawable.debug_background_3, 44 R.drawable.debug_background_4, 45 R.drawable.debug_background_5 46 }; 47 48 /** A simple container for static data in each page */ 49 private static class Page { 50 int titleRes; 51 int textRes; 52 int iconRes; 53 int cardGravity = Gravity.BOTTOM; 54 boolean expansionEnabled = true; 55 float expansionFactor = 1.0f; 56 int expansionDirection = CardFragment.EXPAND_DOWN; 57 Page(int titleRes, int textRes, boolean expansion)58 public Page(int titleRes, int textRes, boolean expansion) { 59 this(titleRes, textRes, 0); 60 this.expansionEnabled = expansion; 61 } 62 Page(int titleRes, int textRes, boolean expansion, float expansionFactor)63 public Page(int titleRes, int textRes, boolean expansion, float expansionFactor) { 64 this(titleRes, textRes, 0); 65 this.expansionEnabled = expansion; 66 this.expansionFactor = expansionFactor; 67 } 68 Page(int titleRes, int textRes, int iconRes)69 public Page(int titleRes, int textRes, int iconRes) { 70 this.titleRes = titleRes; 71 this.textRes = textRes; 72 this.iconRes = iconRes; 73 } 74 Page(int titleRes, int textRes, int iconRes, int gravity)75 public Page(int titleRes, int textRes, int iconRes, int gravity) { 76 this.titleRes = titleRes; 77 this.textRes = textRes; 78 this.iconRes = iconRes; 79 this.cardGravity = gravity; 80 } 81 } 82 83 private final Page[][] PAGES = { 84 { 85 new Page(R.string.welcome_title, R.string.welcome_text, R.drawable.bugdroid, 86 Gravity.CENTER_VERTICAL), 87 }, 88 { 89 new Page(R.string.about_title, R.string.about_text, false), 90 }, 91 { 92 new Page(R.string.cards_title, R.string.cards_text, true, 2), 93 new Page(R.string.expansion_title, R.string.expansion_text, true, 10), 94 }, 95 { 96 new Page(R.string.backgrounds_title, R.string.backgrounds_text, true, 2), 97 new Page(R.string.columns_title, R.string.columns_text, true, 2) 98 }, 99 { 100 new Page(R.string.dismiss_title, R.string.dismiss_text, R.drawable.bugdroid, 101 Gravity.CENTER_VERTICAL), 102 }, 103 104 }; 105 106 @Override getFragment(int row, int col)107 public Fragment getFragment(int row, int col) { 108 Page page = PAGES[row][col]; 109 String title = page.titleRes != 0 ? mContext.getString(page.titleRes) : null; 110 String text = page.textRes != 0 ? mContext.getString(page.textRes) : null; 111 CardFragment fragment = CardFragment.create(title, text, page.iconRes); 112 // Advanced settings 113 fragment.setCardGravity(page.cardGravity); 114 fragment.setExpansionEnabled(page.expansionEnabled); 115 fragment.setExpansionDirection(page.expansionDirection); 116 fragment.setExpansionFactor(page.expansionFactor); 117 return fragment; 118 } 119 120 @Override getBackground(int row, int column)121 public ImageReference getBackground(int row, int column) { 122 return ImageReference.forDrawable(BG_IMAGES[row % BG_IMAGES.length]); 123 } 124 125 @Override getRowCount()126 public int getRowCount() { 127 return PAGES.length; 128 } 129 130 @Override getColumnCount(int rowNum)131 public int getColumnCount(int rowNum) { 132 return PAGES[rowNum].length; 133 } 134 } 135