onCreate正在复制视图 [英] onCreate is duplicating views
问题描述
我正在随意地照着阳光走,我遇到了一个问题.
I'm loosely following the sunshine course and I'm running into a problem.
在我的MainActivity onCreate
方法中,它最初称为
In my MainActivity onCreate
method, it originally called
protected void onCreate(Bundle savedInstanceState) {
mStudentId = Utility.getStudentId(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
根据教程,他们说要添加以下几行
According to the tutorial, they say to add the following lines
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new TimeTableFragment(), TIMETABLEFRAGMENT_TAG)
.commit();
}
结果,现在有两个列表视图彼此重叠,彼此重叠,我看到了以下内容:
As a result, there are now two list views overlayed on top of each other as duplicates, and I see the following:
是什么原因造成的?
Activity_main.xml
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
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
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" />
推荐答案
您将得到两个TimeTableFragment
,因为您在布局中声明了一个,并且在运行时动态地加载了一个.由于您正在学习课程,因此似乎他们开始教您动态Fragment
处理和FragmentTransaction
的知识,因此您可能希望从布局中删除<fragment>
元素.
You're ending up with two TimeTableFragment
s, as you've one declared in the layout, and you're loading one dynamically at runtime. Since you're following a course, it would seem that they're starting to teach you about dynamic Fragment
handling and FragmentTransaction
s, so you probably want to remove the <fragment>
element from the layout.
但是,您的FragmentTransaction
将ID为container
的ViewGroup
指定为Fragment
的容器View
.当前,您已经在CoordinatorLayout
上设置了该ID,这可能并不是您真正想要的.而是用相同的layout_width
和layout_height
将<fragment>
元素替换为<FrameLayout>
,然后将android:id="@+id/container"
属性移至该位置.
However, your FragmentTransaction
is specifying the ViewGroup
with ID container
as the container View
for your Fragment
. Currently, you have that ID set on the CoordinatorLayout
, which is probably not really what you want. Instead, replace the <fragment>
element with a <FrameLayout>
, with the same layout_width
and layout_height
, and move the android:id="@+id/container"
attribute to that.
这篇关于onCreate正在复制视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!