FragmenManager replace makes overlay

前端 未结 3 1446
无人共我
无人共我 2021-01-06 09:59

I\'m using supportlib v4 to reach master-detail flow.

Problem: New instance of \"details\" fragment overlays the first one (xml created) instead replace it.<

相关标签:
3条回答
  • 2021-01-06 10:27

    Here is solution - just need to change fragment to FrameLayout without loading it at activity creation:

    <LinearLayout 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:orientation="horizontal"
        tools:context=".TrackListActivity" >
    
        <fragment
            android:id="@+id/fragmentList"
            android:name="pl.com.digita.BikeComputerUi.TrackList.TrackListFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
    
        <FrameLayout
            android:id="@+id/details"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="2" />
    
    </LinearLayout>
    
    0 讨论(0)
  • 2021-01-06 10:28

    Check following code it will work.

    View fragmentContainer = ClassName.this.findViewById(R.id.fragmentTrack);
     TrackInfoFragment fragment = new TrackInfoFragment();                  
    getSupportFragmentManager().beginTransaction()
    .replace(fragmentContainer.getId(), fragment).commit();     
    
    0 讨论(0)
  • 2021-01-06 10:40

    Note: When you add a fragment to an activity layout by defining the fragment in the layout XML file, you cannot remove the fragment at runtime. If you plan to swap your fragments in and out during user interaction, you must add the fragment to the activity when the activity first starts, as shown in the next lesson.

    Which is very last thing on http://developer.android.com/training/basics/fragments/creating.html

    0 讨论(0)
提交回复
热议问题