gpt4 book ai didi

java - 如何在不使用卡片的情况下在 Android Wear 中使用 GridViewPager?

转载 作者:太空狗 更新时间:2023-10-29 14:11:47 25 4
gpt4 key购买 nike

我是 android 开发的新手,我正在尝试在不使用示例代码中使用的卡片的情况下在 android wear 中创建布局。我想我需要使用 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()));
}
}

感谢您的帮助!

最佳答案

在您的适配器中,按照以下方式提供您的 fragment :

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

然后在您的 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 中使用 GridViewPager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27181874/

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