国际象棋 - GridView,如何通过两次点击移动棋子? [英] Chess - GridView , How to move chess piece by two clicks?

查看:160
本文介绍了国际象棋 - GridView,如何通过两次点击移动棋子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个国际象棋应用程序,它从FEN Notations中加载国际象棋位置



我做了什么的基本信息。



我使用了64个Imageviews的Gridview,并将棋盘图像设置为Gridview的背景图像。我能够在Gridview中加载国际象棋位置。但我不知道如何将棋子从当前位置移动到第一次点击新位置(第二次点击)。但我有我的CustomAdapter的onclick两个职位。



我是android和gridview的新手

解决方案

我已经在MyApplication

中存储了两次点击的位置。 $ p> public class MyApplication extends Application {

private int ClickedImageView1 = -1;
private int ClickedImageView2 = -1;

private static MyApplication instance = new MyApplication();

// Getter-Setters
public static MyApplication getInstance(){
return instance;
}
public static void setInstance(MyApplication instance){
MyApplication.instance = instance;
}


public void setClickedImageView1(int ClickedImageView1){
this.ClickedImageView1 = ClickedImageView1;
}

public int getClickedImageView1(){
return ClickedImageView1;
}

public void setClickedImageView2(int ClickedImageView2){
this.ClickedImageView2 = ClickedImageView2;
}

public int getClickedImageView2(){
return ClickedImageView2;






$ b

同样在CustomAdapter类中,OnClickListener添加了一些代码

  public void onClick(View v){


if(MyApplication.getInstance( ).getClickedImageView1()== -1){
MyApplication.getInstance()。setClickedImageView1(position);
//保存第一次点击位置,如果它是第一次点击
}
else {
MyApplication.getInstance()。setClickedImageView2(position);
//保存第二次点击位置
ImagesId [MyApplication.getInstance()。getClickedImageView2()] = ImagesId [MyApplication.getInstance()。getClickedImageView1()];
//将图片移动到第二个位置
ImagesId [MyApplication.getInstance()。getClickedImageView1()] = 0; //清除imageview1
gridView.setAdapter(new CustomAdapter(MainActivity.this,ImagesId)); //重新加载适配器
// gridView.setBackgroundResource(R.drawable.chessboard);
MyApplication.getInstance()。setClickedImageView1(-1);
MyApplication.getInstance()。setClickedImageView2(-1);
//重置MyApplication对象中的变量
}

它正在工作! !!


I am working with a chess app which loads chess position from FEN Notations

Basic information about what I did..

I used Gridview of 64 Imageviews and set chessboard image as a background image of Gridview.I able to load the chess position in Gridview.But I dont know how to move the chess piece from current location (first click) to new location (second click). but I have the two positions from onclick of my CustomAdapter.

I am new to android and gridview

解决方案

I have stored the position of two imageviews from two clicks in MyApplication
those I got from onclick.

 public class MyApplication extends Application {

    private int ClickedImageView1=-1;
    private int ClickedImageView2=-1;

private static MyApplication instance = new MyApplication();

// Getter-Setters
public static MyApplication getInstance() {
    return instance;
}
public static void setInstance(MyApplication instance) {
    MyApplication.instance = instance;
}


public void setClickedImageView1(int ClickedImageView1) {
    this.ClickedImageView1 = ClickedImageView1;
}

public int getClickedImageView1() {
    return ClickedImageView1;
}

public void setClickedImageView2(int ClickedImageView2) {
    this.ClickedImageView2 = ClickedImageView2;
}

public int getClickedImageView2() {
    return ClickedImageView2;
}
}

Also in the CustomAdapter class, in OnClickListener added some codes

  public void onClick(View v) {


                if (MyApplication.getInstance().getClickedImageView1()== -1) {
                    MyApplication.getInstance().setClickedImageView1(position);  
           //saving the first click position    if it is first click      
                }
                else {
                    MyApplication.getInstance().setClickedImageView2(position);  
        //saving the second click position                                               
                    ImagesId[MyApplication.getInstance().getClickedImageView2()]=ImagesId[MyApplication.getInstance().getClickedImageView1()]; 
              //moving image to secondclick position
                    ImagesId[MyApplication.getInstance().getClickedImageView1()]=0; //clear the imageview1
                    gridView.setAdapter(new CustomAdapter(MainActivity.this,ImagesId));     //reloads the adapter   
             // gridView.setBackgroundResource(R.drawable.chessboard);
                    MyApplication.getInstance().setClickedImageView1(-1);
                    MyApplication.getInstance().setClickedImageView2(-1);
                    //resetting the varibles in MyApplication object
                }

it is working!!!

这篇关于国际象棋 - GridView,如何通过两次点击移动棋子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆