简体   繁体   中英

How to create a full screen Android Wear app without cards

I'm brand new to Android programming and I'm trying to create a full screen Android Wear interface with custom layouts and no cards. I'm trying to go off the sample but I can't figure out how to get around using CardFragments. My MainActivity code is essentially identical to the example, with a few names changed. Here is the code for my GridViewPagerAdaper:

public class MyGridViewPagerAdapter extends FragmentGridPagerAdapter {

private final Context mContext;

public MyGridViewPagerAdapter(Context ctx, FragmentManager fm) {
    super(fm);
    mContext = ctx;
}

static final int[] BG_IMAGES = new int[]{
        R.drawable.bg,
};

/**
 * A simple container for static data in each page
 */
private static class Page {
    int titleRes;
    int textRes;
    int iconRes;
    int cardGravity = Gravity.BOTTOM;
    boolean expansionEnabled = true;
    float expansionFactor = 1.0f;
    int expansionDirection = CardFragment.EXPAND_DOWN;


    public Page(int titleRes) {
        this.titleRes = titleRes;
        this.textRes = textRes;
        this.iconRes = iconRes;
    }

    public Page(int titleRes, int textRes, int iconRes, int gravity) {
        this.titleRes = titleRes;
        this.textRes = textRes;
        this.iconRes = iconRes;
        this.cardGravity = gravity;
    }
}

private final Page[][] PAGES = {
        {
                new Page(R.drawable.tuner),
        },
        {
                new Page(R.drawable.metronome),
                new Page(R.drawable.metroplain)
        },
};
@Override

public Fragment getFragment(int row, int col) {
    Page page = PAGES[row][col];
//        String title = page.titleRes != 0 ? mContext.getString(page.titleRes) : null;
//        String text = page.textRes != 0 ? mContext.getString(page.textRes) : null;
    CardFragment fragment = CardFragment.create("", "", page.iconRes);
    // Advanced settings
    fragment.setCardGravity(page.cardGravity);
    fragment.setExpansionEnabled(page.expansionEnabled);
    fragment.setExpansionDirection(page.expansionDirection);
    fragment.setExpansionFactor(page.expansionFactor);
    return fragment;
}

@Override
public ImageReference getBackground(int row, int column) {
    return ImageReference.forDrawable(BG_IMAGES[row % BG_IMAGES.length]);
}

@Override
public int getRowCount() {
    return PAGES.length;
}

@Override
public int getColumnCount(int rowNum) {
    return PAGES[rowNum].length;
  }
}

What's the best way to get rid of the cards and use a custom layout? Thanks for your help.

In your adapter, give your fragments as following:

private final Activity mContext;
private final Fragment[][] mFragment;

public MyGridViewPagerAdapter(Activity ctx, FragmentManager fm, Fragment[][] fragments)
{
  super(fm);
  mContext = ctx;
  this.mFragment = fragments;
}

then override the following methods:

@Override
public Fragment getFragment(int row, int col) 
{ 
  return mFragment[row][col]; 
}

@Override
public int getRowCount() 
{ 
  return mFragment.length; 
}

@Override
public int getColumnCount(int rowNum) 
{ 
  return mFragment[rowNum].length; 
}

then in your activity do as following:

@Override
protected void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_my);

  final GridViewPager pager = (GridViewPager) findViewById(R.id.pager);
  final Fragment[][] items = {
        {
                CusmtomFragment.newInstance("your","arguments"),
                CusmtomFragment.newInstance()
        },
        {
                OtherFragment.newInstance(),
                AnotherFragment.newInstance(1234)
        }
  };
  // ....
  pager.setAdapter(new MyGridViewPagerAdapter(this, getFragmentManager(), items));
}

Hope it helps !

If you implement a custom wearable app you can do what you want including a custom UI.

The problems are more to Is there any way to detect if the clock is round?

Or how to start the app you n the wearable. This is done with a special service. Basically you is the dataApi for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM