1
我正在使用8x8的網格佈局。對於每個正方形,我使用2個Imageview,一個用於正方形,一個用於該部分。下面是代碼: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和它(我剛剛移動的那部分)消失了半秒鐘(我想適配器正在重新加載整個網格)。
我想這可能與解決方法的工作,如把一張圖片的頂部,直到適配器加載完成,但我想知道什麼,如果我做錯事的最佳解決方案。
下面是網格佈局的適配器代碼:
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:
public void setNewAdapter(Piece[] p){
alp = p;
chessboardGridView.setAdapter(new SquareAdapter(this, p, whiteTurn));
if (whiteTurn == 0){
whiteTurn = 1;
}else{
whiteTurn = 0;
}
}
感謝您的閱讀!
我會創建一個擴展視圖的新類。在onDraw中放幾行代碼繪製棋盤,並在某些數組中存儲關於遊戲所需的任何信息。這將繞過網格佈局和適配器。我看到你有一個工作解決方案。我建議自定義查看方式,因爲它的輕量級,並應通過更多的靈活性給你更多的青貯 – MikeHelland
而且也是一個很好的練習。會嘗試,謝謝。 –