그래서 간단한 보드 게임을 만들어야합니다. 그것은 조각 내의 Gridview (10x10)를 기반으로합니다. 활동은 또한 컨트롤이있는 다른 조각을 표시합니다 (왼쪽 아래로). 컨트롤 조각의 버튼을 눌렀을 때 GridView 내부로 이미지를 이동해야합니다. 지금까지 GridView를 채울 수 있었지만 이미지를 내부로 이동하는 방법을 알 수 없습니다.GridView 내부에서 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;
}
}
어떤 도움이 크게 감사합니다! 주셔서 감사합니다 :)
퍼즐 게임을 만들고 싶습니까? –
사실, 그 캐릭터를 움직일 수있는 더 많은 보드 게임입니다. – Eric