在FragmentManager中发出更改片段的问题

时间:2015-12-30 11:20:46

标签: android android-fragments 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();
		 
	}
	
	
	
	
	
	
	
	
   
}

3 个答案:

答案 0 :(得分:0)

作为Android guide on starting fragments says当您希望更改片段时,不要在布局文件中使用fragment元素。你应该使用一些布局,例如FrameLayout

  

如果您计划在活动期间更改片段,则必须执行此操作。

我认为这是造成问题的原因。

您的新XML布局很接近,但我的意思是INSTEAD完全使用<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时需要创建/附加“home”片段。

public void onCreate(Bundle savedInstanceState) {

添加此项以创建/附加第一个“主页”片段。

 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();
    }

答案 1 :(得分:0)

使用framelayout换行。试试这个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>

答案 2 :(得分:-2)

试试这个可行的代码段

        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();
        }