【问题标题】:How to dynamically replace ImageView Inside a GridView (Android)?如何在 GridView (Android) 中动态替换 ImageView?
【发布时间】:2023-04-09 08:30:01
【问题描述】:

所以我需要创建一个简单的棋盘游戏。它基于片段内的 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;
    }

}

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

【问题讨论】:

  • 你想做益智游戏吗?
  • 不是真的,它更像是一个角色可以移动的棋盘游戏。

标签: java android gridview imageview fragment


【解决方案1】:

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

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

【讨论】:

  • 是的,不幸的是它给了我一个“java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on an null object reference” .
  • 如果您仍在同一个窗口中,您的网格视图怎么会为空?也许你必须宣布它是最终的
  • Eric- 每次都替换片段的答案对于这样一个简单的操作是不需要的,请检查您的代码..
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2015-01-11
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多