Android From gridview layout index convert into row and column

巧了我就是萌 提交于 2019-12-10 10:05:44

问题


If I have index starting from 0, of a position on a grid, how can I turn that into a row, column? I'm getting incorrect results with the following. Fortunately I know how many rows and columns I have and in my case its 11 by 11.

int column = position % columns;

int row = position / columns;

回答1:


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

to reverse:

int index = x + y * columns;

or

int index = (column - 1) + (row - 1) * columns;


来源:https://stackoverflow.com/questions/22494565/android-from-gridview-layout-index-convert-into-row-and-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!