2017-10-29 185 views
0

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

} 

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

+0

你想制作益智游戏吗? – 2017-10-29 04:41:28

+0

不是,它更像是一个可以穿过它的角色的棋盘游戏。 – Eric

回答

-1

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

0

我认为你必须更新每个移动适配器,请尝试更改图像阵列与更新的图像阵列后,加入这行:

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

嗨,你试过了吗?它有帮助吗? – batsheva

+0

是的,不幸的是,它给了我一个“java.lang.NullPointerException:试图调用虚拟方法'无效android.widget.GridView.setAdapter(android.widget.ListAdapter)'对空对象引用”。 – Eric

+0

如果你仍然在同一个窗口中,你的网格视图如何为空? meybe你必须声明它最后 – batsheva