是什么导致 TableLayout 内容消失?
What is causing TableLayout contents to disappear?
我创建了一个扩展 TableLayout 的视图。列数由 XML 属性确定。我在视图上有一个方法,它将列表作为输入,并动态构建 table 行和单元格(扩展行布局,添加它,扩展单元格布局,将它们添加到行),并做一些其他家政事务 - 非常简单。
public class MyTable extends TableLayout {
private int columns = 1;
public MyTable(Context context) {
this(context,null);
}
public MyTable(Context context, AttributeSet attrs) {
super(context,attrs);
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(atters, R.styleable.MyTable);
if (a.hasValue(R.styleable.MyTable_columns)) {
columns = a.getInt(R.styleable.MyTable_columns, 1);
this.setWeightSum(columns);
}
a.recycle();
}
}
public void populate(List<x> items) {
// remove any children, if this was already populated.
this.removeAllViews();
if (items == null || items.size == 0) {
// nothing to do here.
return;
}
int rows = 1;
if (items.size() > columns) {
rows = Math.max(1, items.size / columns + items.size() % columns);
}
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
for (int i = 1; i <= rows; i++) {
TableRow tableRow = (TableRow)layoutInflater.inflate(R.layout.item_row,this,false);
this.addView(tableRow);
int start = (i-1)*columns;
int end = Math.min(items.size()-1,(i * columns)-1);
for (;start <=end;start++) {
LinearLayout cell = (LinearLayout)layoutInflater.inflate(R.layout.item_cell,tableRow,false);
tableRow.addView(cell);
}
}
}
}
我正在片段中使用该小部件。该片段的布局包括我的小部件的一个实例,它似乎工作得很好——我传入我的列表,table 被填充,一切都很顺利——直到我采取导致另一个组件的操作视图被隐藏。例如,我有一个按钮可以执行某些操作,然后将其自身设置为 View.GONE。发生这种情况时,我的 table 布局将从视图中消失。如果我执行另一个修改另一个组件可见性的操作,它会回来,之后它似乎会一直存在。我已经三重检查,我没有在 MyTable 上设置可见性,我什至覆盖了 setVisibility() 来记录 - 但它根本没有被调用。
这里可能发生了什么?
事实证明,我将所有内容都包装在使用 android:height="match_parent" 而不是 wrap_content 设置的滚动视图中。改变了它,它工作正常。
我创建了一个扩展 TableLayout 的视图。列数由 XML 属性确定。我在视图上有一个方法,它将列表作为输入,并动态构建 table 行和单元格(扩展行布局,添加它,扩展单元格布局,将它们添加到行),并做一些其他家政事务 - 非常简单。
public class MyTable extends TableLayout {
private int columns = 1;
public MyTable(Context context) {
this(context,null);
}
public MyTable(Context context, AttributeSet attrs) {
super(context,attrs);
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(atters, R.styleable.MyTable);
if (a.hasValue(R.styleable.MyTable_columns)) {
columns = a.getInt(R.styleable.MyTable_columns, 1);
this.setWeightSum(columns);
}
a.recycle();
}
}
public void populate(List<x> items) {
// remove any children, if this was already populated.
this.removeAllViews();
if (items == null || items.size == 0) {
// nothing to do here.
return;
}
int rows = 1;
if (items.size() > columns) {
rows = Math.max(1, items.size / columns + items.size() % columns);
}
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
for (int i = 1; i <= rows; i++) {
TableRow tableRow = (TableRow)layoutInflater.inflate(R.layout.item_row,this,false);
this.addView(tableRow);
int start = (i-1)*columns;
int end = Math.min(items.size()-1,(i * columns)-1);
for (;start <=end;start++) {
LinearLayout cell = (LinearLayout)layoutInflater.inflate(R.layout.item_cell,tableRow,false);
tableRow.addView(cell);
}
}
}
}
我正在片段中使用该小部件。该片段的布局包括我的小部件的一个实例,它似乎工作得很好——我传入我的列表,table 被填充,一切都很顺利——直到我采取导致另一个组件的操作视图被隐藏。例如,我有一个按钮可以执行某些操作,然后将其自身设置为 View.GONE。发生这种情况时,我的 table 布局将从视图中消失。如果我执行另一个修改另一个组件可见性的操作,它会回来,之后它似乎会一直存在。我已经三重检查,我没有在 MyTable 上设置可见性,我什至覆盖了 setVisibility() 来记录 - 但它根本没有被调用。
这里可能发生了什么?
事实证明,我将所有内容都包装在使用 android:height="match_parent" 而不是 wrap_content 设置的滚动视图中。改变了它,它工作正常。