我是Android开发的新手,我试图在不使用示例代码中使用的卡片的情况下在android中创建布局。我想我需要使用Fragment来做到这一点,但我无法弄清楚如何使用CardFragment。到目前为止,这是我的代码:
MyGridViewPagerAdapter
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.support.wearable.view.CardFragment;
import android.support.wearable.view.FragmentGridPagerAdapter;
import android.support.wearable.view.ImageReference;
import android.view.Gravity;
/**
* Constructs fragments as requested by the GridViewPager. For each row a
* different background is provided.
*/
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, textRes, 0);
this.expansionEnabled = expansion;
}
public Page(int titleRes, int textRes, boolean expansion, float expansionFactor) {
this(titleRes, textRes, 0);
this.expansionEnabled = expansion;
this.expansionFactor = expansionFactor;
}
public Page(int titleRes, int textRes, int iconRes) {
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;
}
}
// Create a static set of pages in a 2D array
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(title, text, page.iconRes);
// Advanced settings (card gravity, card expansion/scrolling)
fragment.setCardGravity(page.cardGravity);
fragment.setExpansionEnabled(page.expansionEnabled);
fragment.setExpansionDirection(page.expansionDirection);
fragment.setExpansionFactor(page.expansionFactor);
return fragment;
}
// Obtain the background image for the page at the specified position
@Override
public ImageReference getBackground(int row, int column) {
return ImageReference.forDrawable(BG_IMAGES[row % BG_IMAGES.length]);
}
// Obtain the number of pages (vertical)
@Override
public int getRowCount() {
return PAGES.length;
}
// Obtain the number of pages (horizontal)
@Override
public int getColumnCount(int rowNum) {
return PAGES[rowNum].length;
}
}
主要
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.wearable.view.GridViewPager;
import android.view.View;
import android.view.View.OnApplyWindowInsetsListener;
import android.view.WindowInsets;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
final Resources res = getResources();
final GridViewPager pager = (GridViewPager) findViewById(R.id.pager);
pager.setOnApplyWindowInsetsListener(new OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
// Adjust page margins:
// A little extra horizontal spacing between pages looks a bit
// less crowded on a round display.
final boolean round = insets.isRound();
int rowMargin = res.getDimensionPixelOffset(R.dimen.page_row_margin);
int colMargin = res.getDimensionPixelOffset(round ?
R.dimen.page_column_margin_round : R.dimen.page_column_margin);
pager.setPageMargins(rowMargin, colMargin);
// GridViewPager relies on insets to properly handle
// layout for round displays. They must be explicitly
// applied since this listener has taken them over.
pager.onApplyWindowInsets(insets);
return insets;
}
});
pager.setAdapter(new MyGridViewPagerAdapter(this, getFragmentManager()));
}
}
感谢您的帮助!
答案 0 :(得分:1)
在适配器中,按以下方式提供片段:
private final Activity mContext;
private final Fragment[][] mFragment;
public MyGridViewPagerAdapter(Activity ctx, FragmentManager fm, Fragment[][] fragments)
{
super(fm);
mContext = ctx;
this.mFragment = fragments;
}
然后覆盖getFragment方法,如下所示:
@Override
public Fragment getFragment(int row, int col)
{
return mFragment[row][col];
}
然后在您的活动中执行以下操作:
@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)
您可以扩展 GridPagerAdapter 而不是FragmentGridPagerAdapter。
例如:
public class MyGridViewPagerAdapter extends GridPagerAdapter {
public MyGridViewPagerAdapter (final Context context) { ... }
@Override
public int getRowCount() { ... }
@Override
public int getColumnCount(int i) { ... }
@Override
public int getCurrentColumnForRow(int row, int currentColumn) { ... }
@Override
protected Object instantiateItem(ViewGroup viewGroup, int row, int col) { ... }
}
密切关注instantiateItem方法。您应该使用它来返回页面的自定义视图(即ImageView或XML视图布局)。
有关更详细的解释和进一步阅读,您可以按照以下链接更详细地解释:
http://www.learnandroidwear.com/sample-3
祝你好运, 安德鲁