我是Android编程的新手,我正在尝试使用自定义布局创建全屏Android Wear界面,而不是卡片。我试图取消样本,但我无法弄清楚如何使用CardFragments。我的MainActivity代码与示例基本相同,更改了一些名称。这是我的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;
}
}
什么是摆脱卡片并使用自定义布局的最佳方法?谢谢你的帮助。
答案 0 :(得分:2)
在适配器中,按以下方式提供片段:
private final Activity mContext;
private final Fragment[][] mFragment;
public MyGridViewPagerAdapter(Activity ctx, FragmentManager fm, Fragment[][] fragments)
{
super(fm);
mContext = ctx;
this.mFragment = fragments;
}
然后覆盖以下方法:
@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;
}
然后在您的活动中执行以下操作:
@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));
}
希望它有所帮助!
答案 1 :(得分:0)
如果您实施自定义可穿戴应用,则可以执行您想要的操作,包括自定义用户界面。
Is there any way to detect if the clock is round?
更多的问题或者如何在可穿戴设备上启动应用程序。这是通过特殊服务完成的。基本上你就是dataApi。