我正在使用8x8的gridlayout。对于每个正方形,我使用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>
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的适配器,它(我刚刚移动的那块)消失了半秒钟(我猜适配器正在重新加载整个网格)。
我想它可以使用一种解决方法,例如将一块图片放在顶部,直到适配器完成加载,但我想知道如果我做错了什么是最好的解决方案。
以下是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;
}
}
感谢阅读!
答案 0 :(得分:2)
这就是我所做的:每次都不刷新适配器。我测试并在拖放时自行移动所有内容,我只是在一开始就为gridlayout设置了一个新的适配器。它的速度要快得多。