简体   繁体   中英

How to dynamically replace ImageView Inside a GridView (Android)?

So I need to create a simple board game. It is based on a Gridview (10x10) inside a fragment.The activity also displays another fragment with controls (up down left right). I need to move the image inside the GridView when a button on my controls fragment is pressed. So far I managed to populate the GridView but I cannot figure out how to move the images inside.

this is the grid fragment

        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;

     }

this is my Adapter:

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

}

Any help will be greatly appreciated ! Thank you :)

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

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

I finally figured it out! I just needed to replace the fragment and commit the transaction when I call goRight() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM