Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Android Wear: How to use a simple a GridViewPager with a FragmentGridPagerAdapter.
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.GridViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true" />
public class GridActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
final GridViewPager mGridPager = (GridViewPager) findViewById(R.id.pager);
mGridPager.setAdapter(new SampleGridPagerAdapter(this, getFragmentManager()));
}
}
public class SampleGridPagerAdapter extends FragmentGridPagerAdapter {
private final Context mContext;
private ArrayList<SimpleRow> mPages;
public SampleGridPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
initPages();
}
private void initPages() {
mPages = new ArrayList<SimpleRow>();
SimpleRow row1 = new SimpleRow();
row1.addPages(new SimplePage("Title1", "Text1", R.drawable.ic_info, R.drawable.hill));
row1.addPages(new SimplePage("Title2", "Text2", R.drawable.ic_tris, R.drawable.img2));
SimpleRow row2 = new SimpleRow();
row2.addPages(new SimplePage("Title3", "Text3", R.drawable.ic_ic_dh_bat, R.drawable.rose));
SimpleRow row3 = new SimpleRow();
row3.addPages(new SimplePage("Title4", "Text4", R.drawable.ic_error_loadingorangesmall, R.drawable.sea));
SimpleRow row4 = new SimpleRow();
row4.addPages(new SimplePage("Title5", "Text5", R.drawable.ic_launcher, R.drawable.water));
row4.addPages(new SimplePage("Title6", "Text6", R.drawable.ic_launcher, R.drawable.mountain));
mPages.add(row1);
mPages.add(row2);
mPages.add(row3);
mPages.add(row4);
}
@Override
public Fragment getFragment(int row, int col) {
SimplePage page = ((SimpleRow)mPages.get(row)).getPages(col);
CardFragment fragment = CardFragment.create(page.mTitle, page.mText, page.mIconId);
return fragment;
}
@Override
public ImageReference getBackground(int row, int col) {
SimplePage page = ((SimpleRow)mPages.get(row)).getPages(col);
return ImageReference.forDrawable(page.mBackgroundId);
}
@Override
public int getRowCount() {
return mPages.size();
}
@Override
public int getColumnCount(int row) {
return mPages.get(row).size();
}
}
public class SimplePage {
public String mTitle;
public String mText;
public int mIconId;
public int mBackgroundId;
public SimplePage(String title, String text, int iconId, int backgroundId) {
this.mTitle = title;
this.mText = text;
this.mIconId = iconId;
this.mBackgroundId = backgroundId;
}
}
public class SimpleRow {
ArrayList<SimplePage> mPagesRow = new ArrayList<SimplePage>();
public void addPages(SimplePage page) {
mPagesRow.add(page);
}
public SimplePage getPages(int index) {
return mPagesRow.get(index);
}
public int size(){
return mPagesRow.size();
}
}

Thank you!

Sees ImageReference has been deprecated. Use this instead

@Override
    public Drawable getBackgroundForPage(int row, int col) {
        SimplePage page = ((SimpleRow)mPages.get(row)).getPages(col);
        Drawable d = mContext.getResources().getDrawable(page.mBackgroundId);
        return d;
    }

Thank you for the code!

On a side note, Resource.getDrawable(int) has been deprecated. Use this instead

@Override
        public Drawable getBackgroundForPage(int row, int col) {
            SimplePage page = ((SimpleRow)mPages.get(row)).getPages(col);
            Drawable d = mContext.getResources().getDrawable(page.mBackgroundId, mContext.getTheme());
            return d;
        } 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment