获取TableLayout中的所有TableRow

eug*_*ene 27 android tablelayout tablerow

我一直在寻找如何在TableLayout中获取所有TableRow的时间.我已经知道如何动态添加和删除行,但我需要遍历所有行并有选择地删除其中的一些行.

我想我可以提出一个解决方案,但我正试图避免在我的应用程序中粗暴的黑客攻击.

Qui*_*son 50

你已经尝试使用getChildCount(),并getChildAt(int)分别?

在循环中应该相当容易:

for(int i = 0, j = table.getChildCount(); i < j; i++) {
    View view = table.getChildAt(i);
    if (view instanceof TableRow) {
        // then, you can remove the the row you want...
        // for instance...
        TableRow row = (TableRow) view;
        if( something you want to check ) {
            table.removeViewAt(i);
            // or...
            table.removeView(row);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 15

如果你有其他类型的视图

TableLayout layout = (TableLayout) findViewById(R.id.IdTable);

for (int i = 0; i < layout.getChildCount(); i++) {
    View child = layout.getChildAt(i);

    if (child instanceof TableRow) {
        TableRow row = (TableRow) child;

        for (int x = 0; x < row.getChildCount(); x++) {
            View view = row.getChildAt(x);
            view.setEnabled(false);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)