onClickListener issue with Android Studio

218 views Asked by At

I've been attempting to build a simple puzzle app, more just to prove to myself that I could than anything else. It's a simple 9 tile puzzle with one tile missing. Tiles adjacent to the missing tile can be moved into that slot.

What I've attempted to do was use a grid layout of 9 imageviews for the tiles. Whenever a tile is to be moved I instantiate a tenth imageview passing that tile's image source and location to it. The original tile is then set to invisible and the mover tile moves to the new location, at which point it passes this data to the recieving tile and goes invisible.

First I find the offset between two adjacent imageViews:

    int startPosition1[] = new int[2];
    topLeft.getLocationOnScreen(startPosition1);
    int startPosition2[] = new int[2];
    topCenter.getLocationOnScreen(startPosition2);

    final int separation = startPosition2[1] - startPosition1[1];

Then, in the OnClickListener, I check to see if the move is valid and call a method I created to do the move (moveme):

    topLeft.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int thisLocation[] = new int[2];
            v.getLocationOnScreen(thisLocation);

            int invisibleLocation[] = new int[2];
            findViewById(theInvisible).getLocationOnScreen(invisibleLocation);

            if ((thisLocation[0] - invisibleLocation[0]) == 0)
            {
                if ((thisLocation[1] - invisibleLocation[1]) == separation)
                {
                    Toast.makeText(getApplicationContext(), R.string.boldly ,Toast.LENGTH_LONG).show();
                    moveMe(thisLocation, "translationY", (0-separation), v);

                }
                else if((invisibleLocation[1] - thisLocation[1]) == separation)
                {
                    Toast.makeText(getApplicationContext(), R.string.boldly, Toast.LENGTH_LONG).show();
                    moveMe(thisLocation, "translationY", separation, v);

                }
            }
            else if ((thisLocation[1] - invisibleLocation[1]) == 0) {
                if ((thisLocation[0] - invisibleLocation[0]) == separation) {
                    Toast.makeText(getApplicationContext(), R.string.boldly, Toast.LENGTH_LONG).show();
                    moveMe(thisLocation, "translationX", (0 - separation), v);


                } else if ((invisibleLocation[0] - thisLocation[0]) == separation) {
                    Toast.makeText(getApplicationContext(), R.string.boldly, Toast.LENGTH_LONG).show();
                    moveMe(thisLocation, "translationX", separation, v);

                }
            }
            else
            {
                Toast.makeText(getApplicationContext(), R.string.illogical, Toast.LENGTH_LONG).show();
            }

        }
    });

Here's the moveMe method:

    void moveMe( int[] thisLocation, String direction, int separation, View v)
{
    Log.i(TAG, "Entere mover");
    ImageView mover = (ImageView) findViewById(R.id.mover);
    ImageView destination = (ImageView) findViewById(getTheInvisible());
    mover.setContentDescription(v.getContentDescription());
    setView(mover);

    mover.setX(thisLocation[0]);
    mover.setY(thisLocation[1]);
    Log.i(TAG, "relocated mover");

    mover.setVisibility(View.VISIBLE);
    v.setVisibility(View.INVISIBLE);
    v.setClickable(false);
    setTheInvisible(v);
    Log.i(TAG, "Swapped visibilities");

    ObjectAnimator test = ObjectAnimator.ofFloat(mover, direction, separation);
    test.setDuration(1000);
    test.setRepeatCount(0);
    test.start();
    Log.i(TAG, "animation complete");

    destination.setContentDescription(mover.getContentDescription());
    setView(destination);
    destination.setVisibility(View.VISIBLE);
    destination.setClickable(true);
    mover.setVisibility(View.INVISIBLE);
    Log.i(TAG, "Swapped views with destination");
}

As near as I can tell, this method is not even being called. Also any tile that fits the criteria to call this method does not issue it's toast message either. Lastly I've noticed that any tile set two spaces away directly along the x or y axis from the blank tile also doesn't display its text. Thanks in advance.

1

There are 1 answers

0
Brandon Thompson On BEST ANSWER

Okay, after nearly a week of looking I found the problem. As it turns out GridLayouts supply their own animations. In order to enable them all you have to do is put this in your GridLayout item in xml:

android:animateLayoutChanges="true"

Then simply make the change and call the layoutAnimator like so:

mover.setLayoutParams(destination.getLayoutParams());
theGrid.startLayoutAnimation();

In this example mover is an ImageView that temporarily takes on the image of other views and moves them to other grid coordinates. theGrid is the resource id name I gave the grid layout.

The issue with it not doing anything for certain tiles was a fault in my if then statements.