Android 方向改变时的 TableLayout 重复
Android TableLayout duplication on orientation change
我在我的应用程序中将 TableLayout 用作简单的 DataGrid。这些行是在代码中添加的。问题是;当我更改屏幕方向时,新的 table 布局出现在旧布局上。
布局文件:
.
.
.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView"
android:fillViewport="false"
>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dataGrid"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle|end">
</TableLayout>
</ScrollView>
</TableLayout>
创建测试条目时调用的函数。我从片段的 public View onCreateView
调用它。
private void fillDataGrid(TableLayout dataGrid,Context context)
{
int columnCount = 5;
int rowCount = 30;
List<TextView> fields = new ArrayList<>();
TableRow row;
dataGrid.removeAllViews();
dataGrid.invalidate();
dataGrid.setStretchAllColumns(true);
for (int j=0;j<rowCount;j++)
{
row = new TableRow(context);
if (j%2 != 0)
row.setBackgroundColor(getResources().
getColor(R.color.highlighted_text_material_light));
for (int i = 0; i < columnCount; i++)
{
TextView field;
field = new TextView(context);
field.setText("field " + i);
fields.add(field);
row.addView(field);
}
dataGrid.addView(row);
}
}
我知道 List<TextView> fields = new ArrayList<>();
的冗余。我留着以后用。
SDK 版本:
minSdkVersion 14
targetSdkVersion 22
错误发生在 android.app.Fragment
class.
到目前为止我尝试过的:
- 为我的数据网格 TableLayout、父 TableLayout 和 ScrollView 调用 invalidate() 和 requestLayout() 方法。
- 尝试了解有多少 TableLayout 或 ScrollView 重复。根据我的测试代码,结果是none。
- 在 xml 中添加行并忽略 fillDataGrid() 函数,并没有解决问题。尽管 xml.
中的行 added/manipulated 只有
我学到了什么:
- 这不是渲染问题。因为当我调用
scrollView.setVisibility(View.INVISIBLE);
时,我仍然可以滚动 duplicated TableLayout。
您可以在下面看到描述情况的屏幕截图。
谢谢。
我假设你那里有内存泄漏......在你的代码中的任何地方都有一个强引用来防止视图被垃圾收集......
您在 Fragment 中提供给 fillDataGrid() 的 dataGrid 对象是什么?也许这是你构造中的缺陷
我找到问题了。我正在使用,
getFragmentManager().beginTransaction()
.add(R.id.mainContainer, fragMain.newInstance("",""))
.commit();
将我的第一个片段附加到 'onCreate()' 方法中的主要 activity。由于 'onCreate()' 是在方向更改时调用的,因此片段被添加了多次。正确的代码是:
getFragmentManager().beginTransaction()
.replace(R.id.mainContainer, fragMain.newInstance("",""))
.commit();
谢谢。
我在我的应用程序中将 TableLayout 用作简单的 DataGrid。这些行是在代码中添加的。问题是;当我更改屏幕方向时,新的 table 布局出现在旧布局上。
布局文件:
.
.
.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView"
android:fillViewport="false"
>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dataGrid"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle|end">
</TableLayout>
</ScrollView>
</TableLayout>
创建测试条目时调用的函数。我从片段的 public View onCreateView
调用它。
private void fillDataGrid(TableLayout dataGrid,Context context)
{
int columnCount = 5;
int rowCount = 30;
List<TextView> fields = new ArrayList<>();
TableRow row;
dataGrid.removeAllViews();
dataGrid.invalidate();
dataGrid.setStretchAllColumns(true);
for (int j=0;j<rowCount;j++)
{
row = new TableRow(context);
if (j%2 != 0)
row.setBackgroundColor(getResources().
getColor(R.color.highlighted_text_material_light));
for (int i = 0; i < columnCount; i++)
{
TextView field;
field = new TextView(context);
field.setText("field " + i);
fields.add(field);
row.addView(field);
}
dataGrid.addView(row);
}
}
我知道 List<TextView> fields = new ArrayList<>();
的冗余。我留着以后用。
SDK 版本:
minSdkVersion 14
targetSdkVersion 22
错误发生在 android.app.Fragment
class.
到目前为止我尝试过的:
- 为我的数据网格 TableLayout、父 TableLayout 和 ScrollView 调用 invalidate() 和 requestLayout() 方法。
- 尝试了解有多少 TableLayout 或 ScrollView 重复。根据我的测试代码,结果是none。
- 在 xml 中添加行并忽略 fillDataGrid() 函数,并没有解决问题。尽管 xml. 中的行 added/manipulated 只有
我学到了什么:
- 这不是渲染问题。因为当我调用
scrollView.setVisibility(View.INVISIBLE);
时,我仍然可以滚动 duplicated TableLayout。
您可以在下面看到描述情况的屏幕截图。
谢谢。
我假设你那里有内存泄漏......在你的代码中的任何地方都有一个强引用来防止视图被垃圾收集......
您在 Fragment 中提供给 fillDataGrid() 的 dataGrid 对象是什么?也许这是你构造中的缺陷
我找到问题了。我正在使用,
getFragmentManager().beginTransaction()
.add(R.id.mainContainer, fragMain.newInstance("",""))
.commit();
将我的第一个片段附加到 'onCreate()' 方法中的主要 activity。由于 'onCreate()' 是在方向更改时调用的,因此片段被添加了多次。正确的代码是:
getFragmentManager().beginTransaction()
.replace(R.id.mainContainer, fragMain.newInstance("",""))
.commit();
谢谢。