0

我正在开发一个游戏,并且我有一个作为 GridView 的库存。每个网格项都是一个 GameObject,单击时会被选中。游戏指示用户可以选择单个对象或组合对象。

我需要的是非常具体的:我希望当用户按下菜单按钮并出现optionsMenu(带有“combine”选项)时,在每个网格项目上动态绘制一个复选框,以便多选一些项目(非常像在图库中,但使用对象而不是图像),然后将它们组合起来。

我正在使用 API 级别 8,所以 MultiChoiceMode 让我很难过。

此外,如果有人对如何实现此功能而不是使用复选框有更好的建议,我肯定会对此持开放态度。提前感谢

这是网格项目的 xml:

<ImageView
    android:id="@+id/grid_item_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<CheckBox
    android:id="@+id/image_check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:visibility="invisible" />

和网格视图:

<TextView
    android:id="@+id/inventoryMsg"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="#000000"
    android:gravity="center"
    android:text="@string/inventory"/>

<GridView
    android:id="@+id/gridView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/inventoryMsg"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="3"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />

这是我的 CustomAdapter 中的 getViewMethod:

public View getView (int position, View convertView, ViewGroup parent) {


            View v = convertView;

    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater layoutInflater = (LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = layoutInflater.inflate(R.layout.griditem, null);
        holder.gameItemCheck = (CheckBox) v.findViewById(R.id.image_check);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }
    holder.gameItemCheck.setId(position);
    holder.gameItemCheck.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            //dosomestuff
        }
    });

    GameObject gameObject = gameObjectList.get(position);
    ImageView gameItemImageView = (ImageView) v.findViewById(R.id.grid_item_image);


    holder.gameItemCheck.setChecked(Inventory.selected[position]);
    holder.id = position;
    return v;
}

public class ViewHolder {

    public CheckBox gameItemCheck;
    int id;
}

我尝试动态更改复选框可见性的方式:

1)

public boolean onKeyDown(int keycode, KeyEvent e) {
    switch (keycode) {
    case KeyEvent.KEYCODE_MENU:
        openOptionsMenu();
        gameObjectAdapter.holder.gameItemCheck.setVisibility(View.VISIBLE);
        gridView.refreshDrawableState();
        return true;
    }

    return super.onKeyDown(keycode, e);
}

2)

public boolean onPrepareOptionsMenu(Menu menu) {
            gameObjectAdapter.holder.gameItemCheck.setVisibility(View.VISIBLE);
            gameObjectAdapter.notifyDataSetChange();
    return true;

}
4

1 回答 1

3

3 Options

Adapt from gallery selection

Method 1:

Just draw a checkbox onto the cell's bitmap when selected. Draw the original when toggle unchecked.
How about using this code:

  private Bitmap drawCheck(Bitmap bmp)
  {
   Bitmap bmChecked = Bitmap.createBitmap(bmp.getWidth(),     bmp.getHeight(), bmp.getConfig());
   Bitmap check = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.upcheck);
   Canvas canvas = new Canvas(bmChecked);

   canvas.drawBitmap(bmp, 0, 0, null);
   canvas.drawBitmap(check, 0, 0, null); 
   return bmChecked;
  }

Method 2

Use the excellent code by Mihai Fonoage.

And then:

  1. added a selected flag to LoadedImage class,
  2. in the onItemClick() function I mark the clicked item as selected
  3. in the adapter's getView() use the flag to i.e. set the image background

IT WORKS.

Method 3: Checkable interface

Have a look at this example -- they use a custom layout class that implements Checkable interface and set a colored background-drawable on checked items.

于 2013-03-21T18:14:37.200 回答