交易后碎片重叠

时间:2016-04-24 14:14:29

标签: android android-fragments fragmenttransaction

我将应用程序的LauncherActivity设为NavigationDrawer Activity。然后我做了两个测试片段:“VibBearFragment”和“FragmentTwo” 我在onNavigationItemSelected()中使用此代码在Fragments之间切换。我在抽屉的内容中加入了“VibBearFragment”,以便在应用程序启动时它就在那里。

 Fragment fragment = new FragmentTwo();
    switch (item.getItemId()) {
        case R.id.nav_teddy_vib:
            fragment = new VibBearFragment();
            break;
        case R.id.nav_fragment1:
            break;
        case R.id.vibActivity:
            Intent i = new Intent(this, MainActivity.class);
            startActivity(i);
            break;
        case R.id.nav_call_me:
            Intent callIntent = new Intent(Intent.ACTION_DIAL);
            callIntent.setData(Uri.parse("tel:0123456789"));
            startActivity(callIntent);

            break;
        default:
            fragment = new FragmentTwo();
            break;
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.vibFragment, fragment).commit();

但是“VibBearFragment”只是掩盖了“FragmentTwo”并且它们重叠。当碎片重叠时所有按钮都有效。有趣的是,如果我点击“FragmentTwo”它们重叠,但是如果我点击“VibBearFragment”即使“FragmentTwo”打开也能正常工作。

这是用语法替换的片段的XML

<RelativeLayout 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/RelativeLayoutSwap"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.slaven.teddybear.Drawer"
tools:showIn="@layout/app_bar_drawer">


<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="com.example.slaven.teddybear.Fragments.VibBearFragment"
    android:id="@+id/vibFragment"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    tools:layout="@layout/vib_bear_fragment" />

我尽力向你展示问题所在。按钮N2(来自图片) 当活动重叠但仍然有效时消失。任何人都可以帮忙吗?

This is the problem

1 个答案:

答案 0 :(得分:2)

您无法使用静态片段(在xml布局中定义的片段)进行事务处理。如果您计划进行交易,请手动添加。

<RelativeLayout 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/RelativeLayoutSwap"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.slaven.teddybear.Drawer"
tools:showIn="@layout/app_bar_drawer">

   <FrameLayout android:id="@+id/content"
      android:layout_width="match_parent"   
      android:layout_height="match_parent"   />

</RelativeLayout>

然后在你的活动onCreate()方法中添加布局中的片段:

//...onCreate()  
Fragment content = getSupportFragmentManager().findFragmentById(R.id.content);
if (savedInstance == null || content == null) {
    // add the initial fragment from the layout
    content = new VibBearFragment();
    getSupportFragmentmanager().beginTransaction().replace(R.id.content, content).commit();
}