在 FragmentManager 中发布更改片段

Issue changing fragment in FragmentManager

New Layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/L1" >
        
        <fragment
            android:id="@+id/fragment_place"
            android:name="com.javacodegeeks.android.fragmentstest.HomeFrag"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        
   </FrameLayout>     

    <RelativeLayout
        android:id="@+id/L1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
        
     
       <ImageView
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/home"
            android:onClick="selectFrag"
            android:layout_alignParentLeft="true"/>
        
         <ImageView
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/search"
            android:onClick="selectFrag"
            android:layout_toRightOf="@+id/button1"/>
         
           <ImageView
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/map"
            android:onClick="selectFrag"
            android:layout_toRightOf="@+id/button2"/>
         
         
    
    
    </RelativeLayout>

</RelativeLayout>

我正在学习使用 FragmentManager 在 ImageView 单击时更改片段。当我单击第一个 imageView 时,带有文本 "home" 的主要片段布局显示良好。但是当点击其他 ImageViews 改变片段页面时,它会改变到新的片段页面但不会删除前一页。结果,上一页与新页面混在一起。 It look like this. 屏幕截图显示主页,文本为 "home",这是上一页和新页面,文本为 "search"。如何在点击新ImageView时移除上一页?

MainActivity

public class MainActivity extends Activity {
 
 private static final int RESULT_SETTINGS = 1;


 @Override
 protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.demo);
      
    
      
 }
 
 
  
 public void selectFrag(View view) {
   Fragment fr = null;
   
   if(view == findViewById(R.id.button1)) {
    fr = new HomeFrag();
   
   }else if(view == findViewById(R.id.button2)){
    fr = new SearchFrag();
   
   }else if(view == findViewById(R.id.button3)){
    fr = new MapFrag();
   
   }
   
   FragmentManager fm = getFragmentManager();
      FragmentTransaction fragmentTransaction = fm.beginTransaction();
      fragmentTransaction.replace(R.id.fragment_place, fr);
      fragmentTransaction.commit();
   
 }
 
 
 
 
 
 
 
 
   
}

as the Android guide on starting fragments says 当您希望片段发生变化时,您不会在布局文件中使用 fragment 元素。你应该使用一些布局,例如 FrameLayout

This is necessary if you plan to change fragments during the life of the activity.

我认为这就是导致您出现问题的原因。


您的新 XML 布局很接近,但我的意思是完全不使用 <fragment>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>  

那么代码本身还有更多工作要做,因为在首次创建 UI 时,您需要 create/attach 'home' 片段。

在你的public void onCreate(Bundle savedInstanceState) {

里面

将此添加到 create/attach 第一个 'home' 片段。

 if (findViewById(R.id.fragment_place) != null) {

        // However, if we're being restored from a previous state,
        // then we don't need to do anything and should return or else
        // we could end up with overlapping fragments.
        if (savedInstanceState != null) {
            return;
        }

        // Create a new Fragment to be placed in the activity layout
        HomeFrag firstFragment = new HomeFrag();

        // In case this activity was started with special instructions from an
        // Intent, pass the Intent's extras to the fragment as arguments
        //   firstFragment.setArguments(getIntent().getExtras());
        // I commented this out since you don't appear to need it right now.

        // Add the fragment to the 'fragment_container' FrameLayout
        getFragmentManager().beginTransaction()
                .add(R.id.fragment_container, firstFragment).commit();
    }

用框架布局包装。试试这个 xml

说明here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Framelayout
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <fragment
        android:id="@+id/fragment_place"
        android:name="com.javacodegeeks.android.fragmentstest.HomeFrag"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/L1" />
  </framelayout>
<RelativeLayout
    android:id="@+id/L1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >


   <ImageView
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/home"
        android:onClick="selectFrag"
        android:layout_alignParentLeft="true"/>

     <ImageView
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/search"
        android:onClick="selectFrag"
        android:layout_toRightOf="@+id/button1"/>

       <ImageView
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/map"
        android:onClick="selectFrag"
        android:layout_toRightOf="@+id/button2"/>




</RelativeLayout>

试试这个有效的代码片段

        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        Fragment currentFragment =   getSupportFragmentManager().findFragmentById(R.id.fragment_place);
        if(currentFragment!=null)
            fragmentTransaction.remove()
        else{
            fragmentTransaction.add(R.id.fragment_place, fr);
            `enter code here`
            fragmentTransaction.commit();
        }