2017-04-25 2 views
0

ich mit einem Schach-App arbeite, die Schach Position von FEN Notations lädtSchach - GridView, Wie man Schachfigur mit zwei Klicken bewegt?

Grundinformationen über das, was ich tat ..

ich Gridview von 64 Imageviews verwendet und Schachbrett Bild als Hintergrundbild von Gridview.I eingestellt kann die Schachposition in Gridview laden. Aber ich weiß nicht, wie man die Schachfigur vom aktuellen Standort (erster Klick) zum neuen Standort (zweiter Klick) bewegt. aber ich habe die zwei Positionen von onclick von meinem CustomAdapter.

Ich bin neu in Android und gridview

Antwort

0

ich die Position von zwei imageviews aus zwei Klicks in MyApplication gespeichert haben
die ich von Onclick bekam.

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

Auch in der CustomAdapter Klasse, hinzugefügt in OnClickListener einige 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 
       } 

es funktioniert !!!

Verwandte Themen