Android如何在gridview布局中找到相邻的项目 [英] Android how to find adjacent items in gridview layout

查看:207
本文介绍了Android如何在gridview布局中找到相邻的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在网格视图布局中获取相邻的项目?目前正在研究可以确定位置相邻项目的功能。我正在减去柱子的位置,当我在侧面和角落时,它显然变得更加复杂。这可能是很多但我现在可以想到的唯一选择,有没有更简单的方法?
我可以从触摸事件中获得位置,矩阵看起来像是这样的位置。

  1 2 3 4 
5 6 7 8
9 10 11 12

以下答案

  boolean isedgeitem(int position)
{
int row = position%11;
int column = position / 11;
int编号= 0;
for(int rowOffset = -1; rowOffset< = 1; rowOffset ++)
{
final int actRow = row + rowOffset;
for(int columnOffset = -1; columnOffset< = 1; columnOffset ++)
{
final int actColumn = column + columnOffset; (actRow> = 0& actRow< 11&& actColumn> = 0&&actColumn< 11)
{
编号++;
}

}
}

if(编号为< 8)
{
return true;
}
else
{
return false;
}
}


解决方案

这:

  // x =列数
// s =索引开始
// a =
的索引// b = b

的索引//如果索引不是从0开始
public static boolean isAdjacent(int x,int s,int a,b)= b-s)/ x,bx =(b-s)%x,by =(b-s)/ x ;
返回a!= b&& Math.abs(ax-bx)< = 1&& Math.abs(ay-by)<= 1;
}

//如果索引从0开始
public static boolean isAdjacent(int x,int a,int b){
int ax = a%x ,ay = a / x,bx = b%x,by = b / x;
返回a!= b&& Math.abs(ax-bx)< = 1&& Math.abs(ay-by)<= 1;
}

考虑gridview布局:



两个单元格相邻,如果:


  • 他们的指数(0〜47)不同
    $差异列数<= 1
  • 差异行号< / li> ; = 1



示例:

 <$ c $ (6,18,12)// true 
isAdjacent(6,18,19)// true
isAdjacent(6,18,24)// true
isAdjacent(6 ,18,17)// false
isAdjacent(6,18,18)// false



<注意:如果第一个单元格索引不是0,则使用第一个方法并使用 s

  • 在这种方法中,对角线被认为是相邻的:

      isAdjacent(6,18,13)// true 
    isAdja cent(6,18,25)// true


    I was wondering on how you can get adjacent item within grid view layout? Currently working on function that can determine the adjacent items from the position. I'm subtracting the position minus the columns and it obviously gets more complicated when I'm on the sides and corners. It might be to much but the only option i can think of now, is there easier way? I can get postions from an touch event and the matrix looks like this with postions.

    1  2  3  4
    5  6  7  8
    9 10 11 12
    

    Answer from below

    boolean isedgeitem(int position) 
        { 
            int row = position % 11;
            int column = position / 11;
            int numberedges = 0;
            for (int rowOffset = -1; rowOffset <= 1; rowOffset++) 
            {
                final int actRow = row + rowOffset;
                for (int columnOffset = -1; columnOffset <= 1; columnOffset++) 
                {
                    final int actColumn = column + columnOffset;
                    if (actRow >= 0 && actRow < 11 && actColumn >= 0 && actColumn < 11) 
                    {
                        numberedges++;
                    }
    
                }
            }
    
            if (numberedges < 8)
            {
                return true;
            }
            else
            {
                return false;
            }
            }
    

    解决方案

    Try this:

    // x = number of columns
    // s = index start
    // a = index of a
    // b = index of b
    
    // if your index doesn't starts at 0
    public static boolean isAdjacent(int x, int s, int a, int b) {
        int ax = (a - s) % x, ay = (a - s) / x, bx = (b - s) % x, by = (b - s) / x;
        return a != b && Math.abs(ax - bx) <= 1 && Math.abs(ay - by) <= 1;
    }
    
    // if your index starts at 0
    public static boolean isAdjacent(int x, int a, int b) {
        int ax = a % x, ay = a / x, bx = b % x, by = b / x;
        return a != b && Math.abs(ax - bx) <= 1 && Math.abs(ay - by) <= 1;
    }
    

    Consider the gridview layout:

    Two cells are adjacent if:

    • their index (0~47) are different
    • difference column number <= 1
    • difference row number <= 1

    Example:

    isAdjacent(6, 18, 12) // true
    isAdjacent(6, 18, 19) // true
    isAdjacent(6, 18, 24) // true
    isAdjacent(6, 18, 17) // false
    isAdjacent(6, 18, 18) // false
    

    Notes:

    1. If the first cell index is not 0, use first method with s argument
    2. In this methods, diagonals are considered as adjacent :

    Example:

    isAdjacent(6, 18, 13) // true
    isAdjacent(6, 18, 25) // true
    

    这篇关于Android如何在gridview布局中找到相邻的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆