gpt4 book ai didi

java - 如何创建无卡全屏 Android Wear 应用

转载 作者:行者123 更新时间:2023-12-02 05:12:39 24 4
gpt4 key购买 nike

我是 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;
}
}

摆脱卡片并使用自定义布局的最佳方法是什么?感谢您的帮助。

最佳答案

在您的适配器中,提供如下 fragment :

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;
}

然后在您的 Activity 中执行以下操作:

@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));
}

希望对你有帮助!

关于java - 如何创建无卡全屏 Android Wear 应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27211037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com