Java Android GridLayout 获取行/列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23154024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:04:53  来源:igfitidea点击:

Android GridLayout get row/column

javaandroidlayoutlistenergrid-layout

提问by VoidCatz

I have a 3x3 GridLayout with buttons. These buttons have a onTileClicked() listener. Now I want to get the position of the clicked button in the grid. Here is my code:

我有一个带按钮的 3x3 GridLayout。这些按钮有一个 onTileClicked() 侦听器。现在我想获得点击按钮在网格中的位置。这是我的代码:

public void onTileClicked(View view) {
    Button button = (Button) view;
    GridLayout.LayoutParams params = (LayoutParams) button.getLayoutParams();
}

But how can i get the row and column of the clicked button? There is params.columnSpec but I don't know what to do with this...

但是我怎样才能得到点击按钮的行和列呢?有 params.columnSpec 但我不知道如何处理这个...

回答by AmmarCSE

There is no direct way to get the position of the cell. However, you can calculate the position of the clicked button assuming you know the number of columns and rows in the onItemClick listener:

没有直接的方法来获取单元格的位置。但是,假设您知道 onItemClick 侦听器中的列数和行数,您可以计算单击按钮的位置:

    public void onItemClick(AdapterView parent, 
         View v, int position, long id) 
    {   
        int row_no=position/3;
        int col_no=position%3;
        .
        .
    }

回答by VoidCatz

Thanks for your answer AmmarCSE, but I solved the broblem by just doing something like this:

感谢您的回答 AmmarCSE,但我通过执行以下操作解决了问题:

public void onTileClicked(View view) {
    Button button = (Button) view;
    if(button.getId()==R.id.btn00)onTileClicked(0, 0, button);
    else if(button.getId()==R.id.btn01)onTileClicked(0, 1, button);
    else if(button.getId()==R.id.btn02)onTileClicked(0, 2, button);
    else if(button.getId()==R.id.btn10)onTileClicked(1, 0, button);
    else if(button.getId()==R.id.btn11)onTileClicked(1, 1, button);
    else if(button.getId()==R.id.btn12)onTileClicked(1, 2, button);
    else if(button.getId()==R.id.btn20)onTileClicked(2, 0, button);
    else if(button.getId()==R.id.btn21)onTileClicked(2, 1, button);
    else if(button.getId()==R.id.btn22)onTileClicked(2, 2, button);
}

回答by LSV

This may be old, but i also looked for a solution and still did not find it. I needed to make multiple lines of button depending on users choice for a game. Using ID's was not possible because the number of buttons could vary from 2x2 to 9x9.

这可能是旧的,但我也寻找了解决方案,但仍然没有找到。我需要根据用户对游戏的选择制作多行按钮。使用 ID 是不可能的,因为按钮的数量可能从 2x2 到 9x9 不等。

My button did not need to have a caption, so here is what i did:

我的按钮不需要有标题,所以这是我所做的:

layout = (GridLayout) findViewById(R.id.layoutPrincipal);
    context = this;        
    for(int i=0;i<rowCount;i++){
        for(int j=0;j<columnCount;j++){
            Button mButon= new Button(this);
            mButon.setText(""+i+""+j);
            mButon.setTextColor(Color.TRANSPARENT);

            mButon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Button button = (Button) v;


                    //button.getText() allows me to retrieve the coordinates.

                    button.getText()

                 }
            });     
            layout.addView(mButon);

        }
    }

Of course this won't work if you need a text on your button. Maybe you could use a tag?

当然,如果您需要按钮上的文字,这将不起作用。也许你可以使用标签?

回答by Xavier Xing

I am facing the same problem and currently the only solution that makes me comfortable is to modify the class and add whatever information you need. My example is

我面临同样的问题,目前唯一让我感到舒服的解决方案是修改类并添加您需要的任何信息。我的例子是

class ImageView extends android.widget.ImageView {
    public ImageView(Context context) {
        super(context);
    }

    //the position of the plane head.
    int xPos,yPos;

    public void setPosInGrid(int x,int y) {
        xPos=x;yPos=y;
    }

    public int[] getPosInGrid() {
        int[] pos=new int[2];
        pos[0]=xPos;
        pos[1]=yPos;
        return pos;
    }
}

And now we can use this additional functionality. We can also rewrite the setLayoutParams and integrate the setPosInGrid() into it. No modification of other codes is required except when you want an ordinary ImageView just use the full identifier "androi.widget.ImageView".

现在我们可以使用这个附加功能。我们还可以重写 setLayoutParams 并将 setPosInGrid() 集成到其中。不需要修改其他代码,除非你想要一个普通的 ImageView 只需使用完整的标识符“androi.widget.ImageView”。