如何在GridView(Android)中动态替换ImageView?

时间:2017-10-29 03:12:39

标签: java android gridview imageview fragment

所以我需要创建一个简单的棋盘游戏。它基于片段内的Gridview(10x10)。活动还显示另一个带控件的片段(从左上方向上)。 当按下我的控件片段上的按钮时,我需要在GridView中移动图像。到目前为止,我设法填充了GridView,但我无法弄清楚如何移动图像。

这是网格片段

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

}

任何帮助将不胜感激!谢谢:)

2 个答案:

答案 0 :(得分:0)

我认为您必须在每次移动中更新适配器,尝试在使用更新的图像阵列更改图像阵列后添加此行:

gridView.setAdapter(new PhotoImageAdapter(this.getActivity(), image));

答案 1 :(得分:-1)

我终于明白了!当我调用goRight()时,我只需要替换片段并提交事务。希望它可以帮助某人。