GridLayout com visualização dinâmica obter linha / coluna

Eu apenas segui issotutorial, para criar um costumeView como um item deGridLayout.

Isso é meuCustomView

public class RowView extends View{

boolean touchOn;
boolean mDownTouch = false;
private OnToggledListener toggledListener;
int _IdRow = 0;
int _IdColumn = 0;

public RowView(Context context, int Rows, int Columns) {
    super(context);
    this._IdRow = Rows;
    this._IdColumn = Columns;
    init();
}

public RowView(Context context) {
    super(context);
    init();
}

public RowView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public RowView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    touchOn = false;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
            MeasureSpec.getSize(heightMeasureSpec));
}

@Override
protected void onDraw(Canvas canvas) {
    if (touchOn) {
        canvas.drawColor(Color.RED);
    } else {
        canvas.drawColor(Color.GRAY);
    }
}
//onClick not possible to use on custom View so, onTouchEvent is the solution
@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);

    switch (event.getAction()) {
        //if Click
        case MotionEvent.ACTION_DOWN:

            touchOn = !touchOn;
            invalidate();

            if(toggledListener != null){
                toggledListener.OnToggled(this, touchOn);
            }

            mDownTouch = true;
            return true;

        case MotionEvent.ACTION_UP:
            if (mDownTouch) {
                mDownTouch = false;
                performClick();
                return true;
            }
    }
    return false;
}

@Override
public boolean performClick() {
    super.performClick();
    return true;
}

public void setOnToggledListener(OnToggledListener listener){
    toggledListener = listener;
}

public int get_IdRow() {
    return _IdRow;
}

public int get_IdColumn() {
    return _IdColumn;
}

Nesta classe, posso detectar quando o usuário clica em um item deGridLayout e mude para outra cor, tudo bem. Mas o problema surge no momento de criar isso:

Este é meuMainActivity onde eu mostroGridLayout :

int numOfCol = mGridLayout.getColumnCount();
    int numOfRow = mGridLayout.getRowCount();
    mRowViews = new RowView[numOfCol*numOfRow];
    for(int yPos=0; yPos<numOfRow; yPos++){
        for(int xPos=0; xPos<numOfCol; xPos++){
            RowView tView = new RowView(this, xPos, yPos);
            tView.setOnToggledListener(this);
            mRowViews[yPos*numOfCol + xPos] = tView;
            mGridLayout.addView(tView);
        }
    }
     mGridLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){

       @Override
       public void onGlobalLayout() {

        final int MARGIN = 5;

        int pWidth = mGridLayout.getWidth();
        int pHeight = mGridLayout.getHeight();
        int numOfCol = mGridLayout.getColumnCount();
        int numOfRow = mGridLayout.getRowCount();
        int w = pWidth/numOfCol;
        int h = pHeight/numOfRow;

        for(int yPos=0; yPos<numOfRow; yPos++){
         for(int xPos=0; xPos<numOfCol; xPos++){
          GridLayout.LayoutParams params =
           (GridLayout.LayoutParams)mRowViews[yPos*numOfCol + xPos].getLayoutParams();
          params.width = w - 2*MARGIN;
          params.height = h - 2*MARGIN;
          params.setMargins(MARGIN, MARGIN, MARGIN, MARGIN);
          mRowViews[yPos*numOfCol + xPos].setLayoutParams(params);
         }
        }
       }});

Também existe um método deInterface OnToggledListener que me dá a linha e a coluna da minhaGridLayout quando um item é clicado:

@Override
public void OnToggled(MyView v, boolean touchOn) {
//get the id string
 String idString = v.get_IdRow() + ":" + v.get_IdColumn();
}

Eu gostaria de evitar criar issomGridLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() porque preenche a tela que eu não quero ... tentei colocarGridLayout 6x6 comandroid:layout_height="400dp" e mostra apenas 3x3 e este é oLogCat mensagem

D / android.widget.GridLayout: restrições verticais: y6-y0> = 1749, y6-y5 <= 291, y5-y4 <= 291, y4-y3 <= 291, y3-y2 <= 291, y2-y1 < = 291, y1-y0 <= 291 são inconsistentes; removendo permanentemente: y6-y5 <= 291.

Eu gostaria de fazer algo comoGridLayout[row][colum] para obter a cor de fundo e depois fazer as coisas, mas não consigo encontrar esta solução.

questionAnswers(1)

yourAnswerToTheQuestion