2017-10-29 2 views
0

그래서 간단한 보드 게임을 만들어야합니다. 그것은 조각 내의 Gridview (10x10)를 기반으로합니다. 활동은 또한 컨트롤이있는 다른 조각을 표시합니다 (왼쪽 아래로). 컨트롤 조각의 버튼을 눌렀을 때 GridView 내부로 이미지를 이동해야합니다. 지금까지 GridView를 채울 수 있었지만 이미지를 내부로 이동하는 방법을 알 수 없습니다.GridView 내부에서 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

퍼즐 게임을 만들고 싶습니까? –

+0

사실, 그 캐릭터를 움직일 수있는 더 많은 보드 게임입니다. – Eric

답변

-1

나는 마침내 그것을 알아 냈어! 난 그냥 조각을 대체하고 goRight()를 호출 할 때 트랜잭션을 커밋해야했습니다. 그것이 누군가를 도울 수 있기를 바랍니다.

0

난 당신이 각 이동에 어댑터를 업데이트 할 필요가 있다고 생각 업데이트 된 이미지 배열로 이미지의 배열을 변경 한 후이 줄을 추가하려고 :

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

안녕하세요, 시도 했습니까? 그것은 도움이 되었습니까? – batsheva

+0

예, 불행히도 그것은 "java.lang.NullPointerException : 가상 메소드 'void android.widget.GridView.setAdapter (android.widget.ListAdapter)'를 null 객체 참조에 호출하려고 시도합니다." – Eric

+0

같은 창에 여전히 있다면 격자보기가 null입니까? 당신은 그것을 최종적으로 선언해야만한다. – batsheva

관련 문제