2017-10-29 239 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

相關問題