所以我需要創建一個簡單的棋盤遊戲。它基於一個片段內的Gridview(10x10)。該活動還顯示了帶有控件的另一個片段(從左下方向右上方)。 當我按下控件片段上的一個按鈕時,我需要移動GridView中的圖像。到目前爲止,我設法填充GridView,但我無法弄清楚如何移動圖像。如何在GridView(Android)中動態替換ImageView?
這是網格片段
public class Grid extends Fragment{
GridView gridView;
private View rootView;
public PhotoImageAdapter mAdapter;
Integer[] image = {
R.drawable.pucman, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground,
R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground,
R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground,
R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground,
R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground,
R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground,
R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground,
R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground,
R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground,
R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground,
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_grid, container, false);
gridView = rootView.findViewById(R.id.GridView);
gridView.setAdapter(new PhotoImageAdapter(this.getActivity(), image));
return rootView;
}
public void goRight()// this function is called by the control fragment.
{
// here is my not so fructuous attempt to replace the image in the
//second cell with the image of my character
image[1]=R.drawable.pucman;
image[0]=R.drawable.ground;
}
這是我的適配器:
public class PhotoImageAdapter extends BaseAdapter {
private Context mContext;
public Integer[] mThumbIds;
public PhotoImageAdapter(Context c, Integer[] image) {
mContext = c;
mThumbIds = image;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(80, 80));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
任何幫助將不勝感激!謝謝:)
你想製作益智遊戲嗎? – 2017-10-29 04:41:28
不是,它更像是一個可以穿過它的角色的棋盤遊戲。 – Eric