Hello, I'm doing a game "Find a couple of numbers." Starring RecyclerView with GridLayoutManager . A field of 6 * 6 chips. I use my model for each item. In the game, I manage the chip states and update the RecyclerView using notifyItemChanged() . Disabled the animations built into the library so as not to interfere. Here is my class for chips

 public class Chip { public static final int CHIP_HIDE = 0; public static final int CHIP_OPEN = 1; public static final int CHIP_CLOSED = 2; public static final int CHIP_UP = 3; public static final int CHIP_NO = 4; int number, drawable, textColor, state, offset; int[] drawables = {R.drawable.bg1, R.drawable.bg2, R.drawable.bg3, R.drawable.bg4}; int[] colors = {Color.WHITE, Color.WHITE, Color.BLACK, Color.WHITE}; Chip(int number){ this.number = number; this.drawable = drawables[number - 1]; this.textColor = colors[number - 1]; this.state = CHIP_NO; } int getNumber(){ return number; } int getDrawable(){ return drawable; } int getTextColor(){ return textColor; } int getState(){ return state; } int getOffset(){ return offset; } void setOffset(int offset){ this.offset = offset; } void setState(int state){ this.state = state; } } 

The chip consists of numbers. That is what I pass to the constructor. Depending on this number, the background is created in the model (there are 4 of them) and the text color. When you create a chip, it is given the status CHIP_NO . I also implemented getter methods and with them in the adapter I create the playing field.

When I open a chip, I change the state to CHIP_OPEN , when I close CHIP_CLOSED , when I delete CHIP_HIDE .

I decided to add a "ladder" animation before starting the game (like dots on the pattern lock screen. First, the upper left chip appears, then the one to the right and below, and so on until the lower right chip). For this, I created another state CHIP_UP and a two-dimensional array (matrix 11 * 6). This is how I cycle through the array and change the states of the chips.

 (new Thread(new Runnable(){ @Override public void run() { try { for(int x = 0; x<ar.length; x++){ for(int y = 0; y<ar[x].length; y++){ list.get(ar[x][y]).setState(Chip.CHIP_UP); list.get(ar[x][y]).setOffset(x*1000); Message msg = h.obtainMessage(UPDATE, ar[x][y],0); h.sendMessage(msg); Log.d(LOG_TAG, "["+x+"]["+y+"] = " + ar[x][y]); } } } catch (Exception e) { Log.d(LOG_TAG, e.toString()); } } })).start(); 

Depending on x I throw a delay in the model class before the animation starts, so that all the chips are not animated at the same time. In the adapte, I set the animations to offset and run. But chips all the same appear all together. What's wrong?(

This is the order in which chips appear.

 1 2,7 3,8,13 4,9,14,19 5,10,15,20,25 6,11,16,21,26,31 12,17,22,27,32 18,23,28,33 24,29,34 30,35 36 

    3 answers 3

      for (int x = 0; x < ar.length; x++) { for (int y = 0; y < ar[x].length; y++) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } list.get(ar[x][y]).setState(Chip.CHIP_UP); list.get(ar[x][y]).setOffset(x * 1000); Message msg = h.obtainMessage(UPDATE, ar[x][y], 0); h.sendMessage(msg); Log.d(LOG_TAG, "[" + x + "][" + y + "] = " + ar[x][y]); } } 
    • I tried, in this case, the chip is shown either three, then two, then one by one. An interesting thing. By the way, you should not fall asleep with every change, but with each passing through the matrix column - Flippy
    • What comes out when you fall asleep between fors? - TimurVI
    • The application just hangs and after it hangs it also opens all the chips at once - Flippy

    as an option to get to the result, without criticism by the number of lines.

      private void setProperties(int num){ list.get(num).setState(Chip.CHIP_UP); list.get(num).setOffset(num * 1000); Message msg = h.obtainMessage(UPDATE, num, 0); h.sendMessage(msg); } private void setSleep(long i){ try { Thread.sleep(i); } catch (InterruptedException e) { e.printStackTrace(); } } 

    and the loop below instead of your two

      for (int i = 1; i < 37; i++) { if (i == 1) { setSleep(100); setProperties(i); Log.i("TAG", "i : - " + i); } else if (i == 2) { setSleep(100); setProperties(i); setProperties(i + 5); Log.i("TAG", "i : - " + i + " , " + (i + 5)); } else if (i == 3) { setSleep(100); setProperties(i); setProperties(i + 5); setProperties(i + 10); Log.i("TAG", "i : - " + i + " , " + (i + 5) + " , " + (i + 10)); } else if (i == 4) { setSleep(100); setProperties(i); setProperties(i + 5); setProperties(i + 10); setProperties(i + 15); Log.i("TAG", "i : - " + i + " , " + (i + 5) + " , " + (i + 10) + " , " + (i + 15)); } else if (i == 5) { setSleep(100); setProperties(i); setProperties(i + 5); setProperties(i + 10); setProperties(i + 15); setProperties(i + 20); Log.i("TAG", "i : - " + i + " , " + (i + 5) + " , " + (i + 10) + " , " + (i + 15) + " , " + (i + 20)); } else if (i == 6) { setSleep(100); setProperties(i); setProperties(i + 5); setProperties(i + 10); setProperties(i + 15); setProperties(i + 20); setProperties(i + 25); Log.i("TAG", "i : - " + i + " , " + (i + 5) + " , " + (i + 10) + " , " + (i + 15) + " , " + (i + 20) + " , " + (i + 25)); } else if (i == 12) { setSleep(100); setProperties(i); setProperties(i + 5); setProperties(i + 10); setProperties(i + 15); setProperties(i + 20); Log.i("TAG", "i : - " + i + " , " + (i + 5) + " , " + (i + 10) + " , " + (i + 15) + " , " + (i + 20)); } else if (i == 18) { setSleep(100); setProperties(i); setProperties(i + 5); setProperties(i + 10); setProperties(i + 15); Log.i("TAG", "i : - " + i + " , " + (i + 5) + " , " + (i + 10) + " , " + (i + 15)); } else if (i == 24) { setSleep(100); setProperties(i); setProperties(i + 5); setProperties(i + 10); Log.i("TAG", "i : - " + i + " , " + (i + 5) + " , " + (i + 10)); } else if (i == 30) { setSleep(100); setProperties(i); setProperties(i + 5); Log.i("TAG", "i : - " + i + " , " + (i + 5)); } else if (i == 36) { setSleep(100); setProperties(i); Log.i("TAG", "i : - " + i); } } 

    Unfortunately, I can not check in

    • I see no reason to do such a cycle. By the way, I just noticed that the matter is in the delay before the animation. I just put it programmatically depending on the position and nothing changes. I don’t understand how to do it (Don’t know where to find the lock screen code in Android Lollipop? - Flippy

    Barely dug. No order is needed, everything is simple. Do so

     Animation animation = AnimationUtils.loadAnimation(this, R.anim.chip_up); GridLayoutAnimationController controller = new GridLayoutAnimationController(animation); controller.setOrder(LayoutAnimationController.ORDER_NORMAL); controller.setDirection(GridLayoutAnimationController.DIRECTION_LEFT_TO_RIGHT | GridLayoutAnimationController.DIRECTION_TOP_TO_BOTTOM); controller.setColumnDelay(0.2f); controller.setRowDelay(0.2f); recyclerView.setLayoutAnimation(controller); 

    Everything!