我正试图使一些逻辑,但我不能达到实际的结果。
我用的是recyclerview(GridLayoutManager).
我有像3*3,5*5,7*7...and这样的网格单元。
图为3*3网格的图像

现在我的情况是什么,我需要的位置,所有最近的单元格。
如图所示,我在下面描述了一些例子:
案例1 :
选定细胞-1 预期结果-2,5,4
案例2:(在图像中)
选定细胞-5 预期结果-1,2,3,4,6,7,8,9
案例3 :
选定细胞-6 预期结果-3,5,9
因此,如何动态地实现所有单元格框的3*3,5*5,7*7....so?
发布于 2017-03-09 15:20:02
我可以给你一个简单的方法来实现这个.not完美,但很容易。
public void getNeareast(int m, int choosedId) {
if (m <= 2) {//too simple ,you can just return the right result;
return;
}
int[] choose = new int[]{-m - 1, -m, -m + 1, -1, 0, +1, m - 1, m, m + 1};
int x = choosedId / m;
int y = choosedId % m;
if (x == 0 || (y == 0 && x == 1)) {//no top data
choose[0] = 0;
choose[1] = 0;
choose[2] = 0;
}
if ((x == m - 1 && y != 0) || x == m) {// no bottom data
choose[6] = 0;
choose[7] = 0;
choose[8] = 0;
}
if (y == 1) { // no left data
choose[0] = 0;
choose[3] = 0;
choose[6] = 0;
}
if (y == 0) {// no right data
choose[2] = 0;
choose[5] = 0;
choose[8] = 0;
}
List<Integer> result = new ArrayList<>();
for (int i = 0; i < 9; i++) {//get choosed data
if (choose[i] != 0) {
result.add(choosedId + choose[i]);
}
}
for (int i : result) {//print result;
Log.d("biu", "biu->" + i);
}
}https://stackoverflow.com/questions/42696389
复制相似问题