如何使用GridViewPager实现带有onClick事件的操作按钮

时间:2015-08-18 06:45:34

标签: android navigation wear-os

我正在实现android GridviewPager android穿戴应用程序。在this code sample的帮助下。

我想显示2个动作按钮,它的onClick事件,它的文本,背景图像。当前代码看起来像圆形磨损模拟器

enter image description here

和方形模拟器

enter image description here

我的问题是。

1)如何删除圆形仿真器中最顶部的黑色部分

2)方形仿真器中的中心图像和文本

3)在操作按钮上执行onClick事件,单击

GridPagerAdapter

public class GridPagerAdapter extends FragmentGridPagerAdapter{

private Context mContext;
private ArrayList<GridRow> mRows;

public GridPagerAdapter(Context mContext, FragmentManager fm) {
    super(fm);
    this.mContext = mContext;
    initAdapter();
}

private void initAdapter() {
    mRows = new ArrayList<GridRow>();
    GridRow row1 = new GridRow();
    row1.addPage(new GridPage("", "            Add Call", R.drawable.addcall, R.mipmap.ic_launcher));
    row1.addPage(new GridPage("", "            View Map", R.drawable.view_map, R.mipmap.ic_launcher));
    mRows.add(row1);

}

@Override
public Fragment instantiateItem(ViewGroup container, int row, int column) {
    Log.d("GridPagerAdapter","in instantiateItem row = "+row+"column = "+column);

    return super.instantiateItem(container, row, column);
}

@Override
public Fragment getFragment(int row, int col) {
    GridPage page = mRows.get(row).getPage(col);
    Log.d("GridPagerAdapter","in getFragment row = "+row+"column = "+col);
    CardFragment cardFragment = CardFragment.create(page.getTitle(), page.getText(), page.getIcon());
    return cardFragment;
}

@Override
public Drawable getBackgroundForPage(int row, int column) {
    GridPage page = mRows.get(row).getPage(column);
    return getBackgroundForRow(page.getBackground());
}

@Override
public int getRowCount() {
    return mRows.size();
}

@Override
public int getColumnCount(int row) {
    return mRows.get(row).getSize();
}
}

磨损活动

public class WearActivity extends Activity {

private GridViewPager mPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wear);

    mPager = (GridViewPager) findViewById(R.id.gridPager);
    mPager.setAdapter(new GridPagerAdapter(this, getFragmentManager()));
}
}

提前感谢您的帮助

1 个答案:

答案 0 :(得分:1)

最后得到答案

1)在xml(rect,round)之一中添加以下代码

<ImageView
    android:id="@+id/imgview"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginTop="35dp"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/card1"/>

<TextView android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_gravity="center_horizontal"
    android:layout_height="wrap_content"
    android:text="@string/hello_square" />

2)编辑instantiateItem方法

   @Override
    public Object instantiateItem(ViewGroup viewGroup, final int row, final int col) {
     final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.rect_activity_wear, viewGroup, false);
        final TextView textView = (TextView) view.findViewById(R.id.text);
        final ImageView imageView = (ImageView) view.findViewById(R.id.imgview);

        imageView.setImageResource(carImageIDs[row][col]);
        textView.setText(ImageTexts[row][col]);

         viewGroup.addView(view);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("Wear","Image Click row = "+row+"col =  "+col);
            }
        });

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("Wear","text Click row = "+row+"col =  "+col);
            }
        });

        return view;
     }

希望它有助于某人,快乐编码