如果我有从 0 开始的索引,在网格上的一个位置,我怎样才能把它变成一行,一列?我得到以下结果不正确。幸运的是,我知道我有多少行和列,在我的例子中是 11 x 11。
int column = position % columns;
int row = position / columns;
最佳答案
// x : horizontal position in range [0; columns-1]
// y : vertical position in range [0; rows-1]
int x = index % columns;
int y = index / columns;
int column = x + 1;
int row = y + 1;
反转:
int index = x + y * columns;
或
int index = (column - 1) + (row - 1) * columns;
关于Android从gridview布局索引转换成行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22494565/