2014-01-17 5 views
1

저는 8x8의 그리드 레이 아웃을 사용하고 있습니다. 각 사각형에 대해 저는 2 개의 이미지 뷰를 사용합니다. 하나는 사각형을위한 것이고 하나는 조각을위한 것입니다.체스 보드 on android

activity_chess.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".ChessActivity" > 

<GridView 
    android:id="@+id/chessboard" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#000000" 
    android:gravity="center_horizontal" 
    android:numColumns="8" > 
</GridView> 

square.xml :

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/square" 
    android:layout_width="35dp" 
    android:layout_height="35dp" 
    android:background="#000080" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/square_background" 
     android:layout_width="35dp" 
     android:layout_height="35dp" /> 

    <ImageView 
     android:id="@+id/piece" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" /> 

</FrameLayout> 

내 문제가

매번 내가 조각을 이동하고있어 여기에 코드입니다 이동이 합법적 인 경우 어댑터를 다시로드합니다. gridlayout, 그리고 그것 (내가 방금 옮긴 조각)은 0.5 초 동안 사라집니다 (나는 어댑터가 전체 그리드를 다시로드하고있는 것 같습니다).

어댑터가로드 될 때까지 상단에 그림을 올려 놓는 것과 같은 해결 방법으로 작동 할 수 있지만 뭔가 잘못하고있는 경우 가장 좋은 해결책이 무엇인지 알고 싶습니다. 읽기에 대한

public void setNewAdapter(Piece[] p){ 
    alp = p; 
    chessboardGridView.setAdapter(new SquareAdapter(this, p, whiteTurn)); 
    if (whiteTurn == 0){ 
     whiteTurn = 1; 
    }else{ 
     whiteTurn = 0; 
    } 

} 

감사 :

여기
package com.example.adapter; 

import com.example.business.King; 
import com.example.business.Piece; 
import com.example.lessonchess.ChessActivity; 
import com.example.lessonchess.R; 

import android.content.ClipData; 
import android.content.Context; 
import android.util.Log; 
import android.view.DragEvent; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.DragShadowBuilder; 
import android.view.View.OnDragListener; 
import android.view.View.OnTouchListener; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.FrameLayout; 
import android.widget.ImageView; 

public class SquareAdapter extends BaseAdapter{ 

private Context mContext; 
private LayoutInflater mInflater; 
private Piece[] lp; 

Piece currentPiece = null; 

FrameLayout flcp; 
ImageView imgvcp = null; 

private int whiteTurn; 


// CHESSBOARD 
// references to our images 
private Integer[] chessboardIds = { 
     R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, 
     R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, 
     R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, 
     R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, 
     R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, 
     R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, 
     R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, 
     R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, 
     R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, 
     R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, 
     R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, 
     R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, 
     R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, 
     R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, 
     R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, 
     R.drawable.darksquare, R.drawable.lightsquare, R.drawable.darksquare, R.drawable.lightsquare, 
}; 

static class ViewHolder { 
    public ImageView square; 
    public ImageView piece; 
} 


public SquareAdapter(Context c, Piece[] listPiece, int turn) { 
    mContext = c; 
    Context context = c.getApplicationContext(); 
    mInflater = LayoutInflater.from(context); 
    lp = listPiece; 
    whiteTurn = turn; 
} 

@Override 
public int getCount() { 
    return chessboardIds.length; 
} 

@Override 
public Object getItem(int arg0) { 
    return null; 
} 

@Override 
public long getItemId(int arg0) { 
    return 0; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View rowView = convertView; 
    if (rowView == null) { // if it's not recycled, initialize some attributes 

     rowView = mInflater.inflate(R.layout.square, null); 


     ViewHolder viewHolder = new ViewHolder(); 
     viewHolder.square = (ImageView) rowView.findViewById(R.id.square_background); 
     viewHolder.square.setImageResource(chessboardIds[position]); 
     viewHolder.piece = (ImageView) rowView.findViewById(R.id.piece); 
     viewHolder.piece.setImageResource(lp[position].getRessource()); 

     lp[position].setCurrentSquare(position); 

     // Assign the touch listener to your view which you want to move 
     viewHolder.piece.setOnTouchListener(new MyTouchListener()); 

     viewHolder.square.setOnDragListener(new MyDragListener()); 

     rowView.setTag(viewHolder); 
    } 

    ViewHolder holder = (ViewHolder) rowView.getTag(); 

//  if(lp[position] != null){ 
//   holder.piece.setImageResource(((Piece) lp[position]).getRessource()); 

//  } 



    //  if(currentPiece == null){ 
    //   currentPiece = new Piece(); 
    //  }else{ 
    //   Log.v("Test", "Test class " + `lp[position].getClass().toString());` 
    //  } 

//  lp[position].setCurrentSquare(position); 
     holder.piece.setTag(lp[position]); 

    return rowView; 
} 


// This defines your touch listener 
private final class MyTouchListener implements OnTouchListener { 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
      ClipData data = ClipData.newPlainText("", ""); 
      DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
      view.startDrag(data, shadowBuilder, view, 0); 
      view.setVisibility(View.INVISIBLE); 


      flcp = (FrameLayout) view.getParent(); 
      imgvcp = (ImageView) flcp.getChildAt(1); 
      currentPiece = (Piece) view.getTag(); 

      //    ((ChessActivity) mContext).setNewAdapter(((Piece) view.getTag()).getPossibleMoves()); 

      return true; 
     } else { 
      return false; 
     } 
    } 
} 

class MyDragListener implements OnDragListener { 

    @Override 
    public boolean onDrag(View v, DragEvent event) { 
     int action = event.getAction(); 

     FrameLayout fl2; 
     ImageView imgv2; 

     switch (event.getAction()) { 
     case DragEvent.ACTION_DRAG_STARTED: 
      //    Log.v("Test", "Entered start"); 
      break; 
     case DragEvent.ACTION_DRAG_ENTERED: 
      //    Log.v("Test", "Entered drag"); 
      break; 
     case DragEvent.ACTION_DRAG_EXITED: 
      break; 
     case DragEvent.ACTION_DROP: 
      //    Log.v("Test", "Entered drop"); 
      fl2 = (FrameLayout) v.getParent(); 
      imgv2 = (ImageView) fl2.getChildAt(1); 
      Piece square = (Piece) imgv2.getTag(); 

      if(currentPiece.getPossibleMoves().contains(square.getCurrentSquare())){ 
       //     imgv.setImageResource(currentPiece.getRessource()); 
       Piece destinationPiece = lp[square.getCurrentSquare()]; 
       lp[square.getCurrentSquare()] = currentPiece; 
       lp[currentPiece.getCurrentSquare()] = new Piece(); 

       if((yourBeingCheckedDumbAss(currentPiece)) 
         ||((destinationPiece instanceof King) 
           &&(destinationPiece.getColor() != currentPiece.getColor())) 
           ||(whiteTurn != currentPiece.getColor())){ 
        imgvcp.setImageResource(currentPiece.getRessource()); 
        imgvcp.setVisibility(View.VISIBLE); 
        lp[square.getCurrentSquare()] = destinationPiece; 
        lp[currentPiece.getCurrentSquare()] = currentPiece; 
       }else{ 
        imgvcp.setImageResource(currentPiece.getRessource()); 
        ((ChessActivity) mContext).setNewAdapter(lp); 
       } 

      }else{ 
       imgvcp.setVisibility(View.VISIBLE); 
      } 

      break; 
     case DragEvent.ACTION_DRAG_ENDED: 
     default: 
      break; 
     } 
     return true; 
    } 

    public boolean movementValid(){ 

     return false; 

    } 
} 


public boolean yourBeingCheckedDumbAss(Piece p){ 

    boolean isCheck = false; 

    int positionOfMyKing = 0; 


    for (int i = 0; i <= 63; i++){ 
     if ((lp[i].getColor()== p.getColor() 
       && (lp[i] instanceof King))){ 
      positionOfMyKing = i; 
     } 
    } 

    for (int i = 0; i <= 63; i++){ 
     if ((lp[i].getColor()!= -1) 
       && (lp[i].getColor()!= p.getColor())){ 
      if(lp[i].getPossibleMoves().contains(positionOfMyKing)){ 
       isCheck = true; 
      } 
     } 

    } 

    return isCheck; 



} 


} 

가 ChessActivity.xml의 방법 setNewAdapter입니다 :

다음은 GridLayout과의 어댑터에 대한 코드입니다!

+2

View를 확장하는 새로운 클래스를 만들 것입니다. onDraw에 몇 줄의 코드를 넣어 체커 보드를 페인트하고 게임에 대해 필요한 정보를 배열에 저장하십시오. 이는 그리드 레이아웃과 어댑터를 우회합니다. 네가 일하는 해결책을 찾은 것 같아. 사용자 정의보기 방법을 제안합니다. 가볍고 유연성을 더 많이 부여해야하기 때문입니다. – MikeHelland

+0

좋은 운동이 될 수도 있습니다. 해봐, 고마워. –

답변

1

다음은 내가 한 일입니다. 매번 어댑터를 새로 고치지 마십시오. 테스트를하고 드래그 앤 드롭에 모든 것을 직접 옮기고, 처음에는 그리드 레이 아웃 용 새 어댑터를 설정했습니다. 그것은 훨씬 빠릅니다.

관련 문제