[英]How can I use GridViewPager in Android Wear without using cards?
I'm brand new to android development and I'm trying to create a layout in android wear without using the cards that are used in the sample code. 我是android开发的新手,我试图在不使用示例代码中使用的卡片的情况下在android中创建一个布局。 I think I need to use a Fragment to do this, but I can't figure out how to get around using a CardFragment. 我想我需要使用Fragment来做到这一点,但我无法弄清楚如何使用CardFragment。 Here's my code so far: 到目前为止,这是我的代码:
MyGridViewPagerAdapter 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;
}
}
Main 主要
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()));
}
}
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 getFragment method as following: 然后重写getFragment方法,如下所示:
@Override
public Fragment getFragment(int row, int col)
{
return mFragment[row][col];
}
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 ! 希望能帮助到你 !
You can extend GridPagerAdapter instead of FragmentGridPagerAdapter. 您可以扩展GridPagerAdapter而不是FragmentGridPagerAdapter。
For example: 例如:
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) { ... } }
Pay close attention to the instantiateItem method. 密切关注instantiateItem方法。 You should be using it to return a custom view (ie ImageView or XML View Layout) for your pages. 您应该使用它来返回页面的自定义视图(即ImageView或XML视图布局)。
For a more detailed explanation and further reading, you can follow the below link which explains things in more detail: 有关更详细的说明和进一步阅读,您可以按照以下链接更详细地解释事情:
http://www.learnandroidwear.com/sample-3 http://www.learnandroidwear.com/sample-3
Best of luck, Andrew 祝你好运,安德鲁
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.