android - 安卓棋盘

标签 android grid-layout

我使用的是 8x8 的网格布局。对于每个正方形,我使用 2 个 Imageview,一个用于正方形,一个用于棋子。这是代码:

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>

方形.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> 

我的问题

每次我移动一 block 并且移动是合法的,我重新加载网格布局的适配器,它(我刚刚移动的那 block )消失半秒钟(我猜适配器正在重新加载整个网格)。

我想它可以使用变通方法,例如在适配器加载完成之前将图片放在顶部,但我想知道如果我做错了什么,最好的解决方案是什么。

这是网格布局适配器的代码:

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;
    }

}

感谢阅读!

最佳答案

这是我所做的:只是不要每次都刷新适配器。我自己测试并通过拖放移动所有内容,我只是在一开始就为网格布局设置了一个新的适配器。它要快得多。

关于android - 安卓棋盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21184264/

相关文章:

java - 使用 GSON 解析 JSON 内部字段

java - 在 fragment Activity 中使用列表适配器

android - 适用于 Android 的替代应用内购买方法

java - 如何使用 GridLayout 或 GridBagLayout 垂直填充 JPanel?

android - 通过在外部单击可以关闭 PopupMenu 的方式关闭 PopupWindow

android - 像android中的常规一样工作的不可见/透明按钮?

java - 如何使用 GridLayout 中的特定元素?

Java:如何在 GridLayout 中嵌套 JPanel?

java - 如何设置使用 gridLayout 的 JPanel 最大或首选尺寸?