onCreate正在复制视图

时间:2016-06-16 23:17:40

标签: android android-layout android-fragments

我松散地追随着阳光,我遇到了一个问题。

在我的MainActivity onCreate方法中,它最初称为

  protected void onCreate(Bundle savedInstanceState) {
    mStudentId = Utility.getStudentId(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

根据教程,他们说要添加以下行

if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new TimeTableFragment(), TIMETABLEFRAGMENT_TAG)
                .commit();
    }

结果,现在有两个列表视图叠加在一起作为重复,我看到以下内容:

enter image description here

造成这种情况的原因是什么?

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment"
    android:name="com.mcgowan.timetable.itsligotimetables.TimeTableFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:layout="@layout/fragment_main" />


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

activity_detail.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.sunshine.app.DetailActivity"
    tools:ignore="MergeRootFrame" />


<include layout="@layout/fragment_detail" />

<!--<android.support.design.widget.FloatingActionButton-->
<!--android:id="@+id/fab"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_gravity="bottom|end"-->
<!--android:layout_margin="@dimen/fab_margin"-->
<!--android:src="@android:drawable/ic_dialog_email" />-->

fragment_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TimeTableFragment"
tools:showIn="@layout/activity_main">


<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listview_timetable" />

2 个答案:

答案 0 :(得分:3)

你最终得到两个TimeTableFragment,因为你在布局中声明了一个,并且你在运行时动态加载一个。由于您正在学习某门课程,因此他们似乎开始教您动态Fragment处理和FragmentTransaction,因此您可能希望从中移除<fragment>元素布局。

但是,您的FragmentTransaction正在为ViewGroup指定标识为container的{​​{1}}作为容器View。目前,您在Fragment上设置了该ID,这可能不是您想要的。相反,将CoordinatorLayout元素替换为<fragment>,使用相同的<FrameLayout>layout_width,并将layout_height属性移动到该属性。

答案 1 :(得分:0)

默认情况下,设备轮换后应恢复碎片。

编辑:删除示例代码

当然,您需要将数据保存在savedInstanceState包中