简体   繁体   English

如何在Android的动态表格布局中了解单元格位置

[英]How to get to know cell position in dynamic Table Layout in android

Here I am using dynamic table layout but i am not able to get cell position. 在这里,我使用动态表格布局,但无法获取单元格位置。 My question is "How to get to row and column position" in dynamic table Layout in android. 我的问题是android动态表格布局中的“如何到达行和列的位置”。 Please help me. 请帮我。

Thanks. 谢谢。

for (int i = 1; i <= rows; i++) {

        TableRow row = new TableRow(this);
        row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));

        // inner for loop
        for (int j = 1; j <= cols; j++) {

             TextView tv = new TextView(this);
            tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                     LayoutParams.WRAP_CONTENT));
            tv.setBackgroundResource(R.drawable.cell_shape);
            tv.setPadding(5, 5, 5, 5);
            tv.setId(j);
            tv.setText("R " + i + ", C" + j);

            //mOrangeToolTipView = mToolTipFrameLayout.showToolTipForView(toolTip, tv);
            tv.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(final View v) {

                    Log.d("Row and Column position ", v.getTouchables()+"");

                }
            });

            row.addView(tv);

        }

        table_layout.addView(row);

    }

Probably its too late for you, but... To get to row and column position from where?? 对您来说可能为时已晚,但是...要从哪里到达行和列的位置? From the inside of your inner for-loop the row position is i and the column position is j . 从内部for循环的内部开始,行位置是i ,列位置是j Outside try something like 在外面尝试像

((TextView)((TableRow)tableLayout.getChildAt(row)).getChildAt(column)).setText("MyText");

If you want to set different OnClickListeners for different cells, trying to access i or j from the inner class (new View.OnClickListener(){...}), I have bad news for you, its not possible. 如果要为不同的单元格设置不同的OnClickListener,尝试从内部类(新View.OnClickListener(){...})访问ij ,则对您来说有个坏消息,这是不可能的。 But you create every cell in a loop, so you can do it like 但是您可以循环创建每个单元格,因此可以像

...
if (i == 1 && j == 1){
                    tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            // do what cell (1,1) have to do
                        }
                    });
                } else {
                    tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            // do what other cells have to do
                        }
                    });
                }
...

I hope it will help you or some others ;) 我希望它能对您或其他人有所帮助;)

Got stuck in same problem ...found a solution which worked for me but .....it is not applicable in all cases . 陷入了同样的问题...找到了一个对我有用的解决方案,但是.....它不适用于所有情况

Changed this line : tv.setId(j); 更改此行: tv.setId(j); To : tv.setId((i*100)+j); 至: tv.setId((i*100)+j);

Logic of how it works: 运作方式的逻辑:

To get row index : row_index = id/100; 获取行索引: row_index = id/100; To get column index : column_index = id%100; 获取列索引: column_index = id%100; Note : it works give i and j are 2 digit number.(Guess as it is indices to locate a textview in a tablelayout,it would be within 99 ,at max) 注意:它的工作原理是给i和j是2位数字。(猜想它是在表格布局中定位textview的索引,最大应在99以内)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM