代码之家  ›  专栏  ›  技术社区  ›  Eric

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

  •  0
  • Eric  · 技术社区  · 8 年前

    所以我需要创建一个简单的棋盘游戏。它基于片段内的网格视图(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 回复  |  直到 8 年前
        1
  •  0
  •   batsheva    8 年前

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

    gridView.setAdapter(new PhotoImageAdapter(this.getActivity(), image));
    
        2
  •  -1
  •   double-beep hudson solomon    6 年前

    我终于明白了!我只需要在调用时替换片段并提交事务 goRight() .

    推荐文章