方向改变时的Android桌面布局复制

方向改变时的Android桌面布局复制,android,datagrid,fragment,screen-orientation,tablelayout,Android,Datagrid,Fragment,Screen Orientation,Tablelayout,在我的应用程序中,我将TableLayout用作一个简单的数据网格。这些行是在代码中添加的。问题是,;当我改变屏幕方向时,一个新的表格布局出现在旧的表格布局上 布局文件: . . . <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/scrollView" android:fillViewport="fal

在我的应用程序中,我将TableLayout用作一个简单的数据网格。这些行是在代码中添加的。问题是,;当我改变屏幕方向时,一个新的表格布局出现在旧的表格布局上

布局文件:

.
.
.
<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>
我知道
List fields=new ArrayList()的冗余性。我留着以后用

sdk版本:

    minSdkVersion 14
    targetSdkVersion 22
错误发生在一个
android.app.Fragment
类中

到目前为止,我所尝试的:

  • 为我的datagrid TableLayout、父TableLayout和ScrollView调用invalidate()和requestLayout()方法
  • 尝试了解复制了多少TableLayouts或ScrollView。根据我的测试代码,结果是
  • 在xml中添加行并忽略fillDataGrid()函数并没有解决问题。尽管行仅在xml中添加/操作
  • 我学到的是:

    • 这不是渲染问题。因为当我调用
      scrollView.setVisibility(View.INVISIBLE)时我仍然可以滚动复制的表格布局
    您可以看到下面描述情况的屏幕截图

    多谢各位


    我假设您有内存泄漏……代码中的任何地方都有一个强引用,可以防止视图被垃圾收集


    在片段中为fillDataGrid()提供的dataGrid对象是什么?也许这就是你的结构中的缺陷,我发现了问题所在。我用的是

    getFragmentManager().beginTransaction()
                            .add(R.id.mainContainer, fragMain.newInstance("",""))
                            .commit();
    
    将我的第一个片段附加到“onCreate()”方法中的主活动。由于在方向更改时调用了“onCreate()”,因此多次添加了该片段。正确的代码是:

    getFragmentManager().beginTransaction()
                            .replace(R.id.mainContainer, fragMain.newInstance("",""))
                            .commit();
    

    谢谢。

    “私有静态表布局数据网格”是我的声明。我使它成为一个局部变量和非静态变量,但它不起作用。另外,我忘了提到我在xml文件中手动添加了行,并且没有在代码中操作TableLayout,因此发生了错误。那么,做一个垫子,找出引用的位置:请参见此处:或此处:
    getFragmentManager().beginTransaction()
                            .replace(R.id.mainContainer, fragMain.newInstance("",""))
                            .commit();