我使用导航抽屉和FrameLayout创建了一个活动。此框架布局应包含与所选导航项对应的片段。所以我按照android教程中的说明操作,但它不起作用。
问题: 在我看来,片段只是添加而不是替换。我做错了什么?
我的活动xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainViewDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="@+id/mainViewContentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/drawer_content_padding" >
</FrameLayout>
<ListView android:id="@+id/mainViewDrawerList"
android:layout_width="@dimen/drawer_size"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="none"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="@color/drawer_background"
/>
</android.support.v4.widget.DrawerLayout>
我的第一个片段xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_horizontal_margin"
android:id="@id/fragmentProfile">
<TextView
style="@android:style/TextAppearance.Small"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mainViewProfileGivenNameLabel"
/>
<TextView
android:id="@+id/profileGivenNameTextView"
style="@android:style/TextAppearance.Large"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
我的第二个片段xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@id/fragmentProtocol">
<ListView android:id="@+id/protocolTableListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:transcriptMode="alwaysScroll"
android:cacheColorHint="#00000000"
android:listSelector="@android:color/transparent"/>
</LinearLayout>
活动中的转换方法:
private void switchToFragment(int navigationId, boolean addToBackStack){
FragmentManager fragmentManager = getFragmentManager();
String fragmentTag = null;
boolean fragmentCreated = false;
switch(navigationId) {
case NavigationItemProvider.NAVIGATIONITEM_PROFILE:
fragmentTag = NAVIGATIONTAG_PROFILE;
break;
case NavigationItemProvider.NAVIGATIONITEM_PROTOCOL:
fragmentTag = NAVIGATIONTAG_PROTOCOL;
break;
}
// determine if the fragment is already instanciated otherwise create
FragmentBase fragment = (FragmentBase)fragmentManager.findFragmentByTag(fragmentTag);
if (fragment == null){
if (fragmentTag == NAVIGATIONTAG_PROFILE) {
fragment = new ProfileFragment();
fragmentCreated = true;
}
else if(fragmentTag == NAVIGATIONTAG_PROTOCOL) {
fragment = new ProtocolFragment();
fragmentCreated = true;
}
}
// switch to fragment
if (fragment.isVisible()){
return;
}
FragmentTransaction transaction = fragmentManager.beginTransaction();
if (fragmentCreated) {
transaction.replace(R.id.mainViewContentFrame, fragment, fragmentTag);
}
else{
transaction.show(fragment);
}
if(addToBackStack)
{
transaction.addToBackStack(null);
}
transaction.commit();
}
答案 0 :(得分:1)
解决了这个问题: 在onCreate中,我返回了super.onCreate的结果而不是Layoutinfalter.inflate的结果,另外我没有调用inflate,最后一个参数设置为false。