在FragmentManager中更改Fragment的问题

3

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”,效果良好。但是当我点击其他ImageView以更改片段页面时,它会更改到新的片段页面,但不会删除先前的页面。结果,先前的页面与新页面混合在一起。 这是它的样子。 屏幕截图显示带有文本“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();
   
 }
 
 
 
 
 
 
 
 
   
}


1
使用FrameLayout内的Fragment,请参考此答案 - Zeeshan Shabbir
3个回答

0

正如 Android启动片段指南所说的那样,当您希望片段更改时,不应在布局文件中使用fragment元素,而应使用一些布局,如FrameLayout

如果您计划在活动生命周期内更改片段,则这是必要的。

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


你的新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时创建/附加“主页”片段。

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

这里有一个很好的解释,说明为什么你应该使用FrameLayout。https://dev59.com/l2Mm5IYBdhLWcg3wlv0K#17495250 - mawalker
我添加了FrameLayout并在其中插入了片段,但问题仍然存在。我已经在我的帖子中编辑了布局代码。@mawalker - Sammy
非常感谢。它完美地运行了。@mawalker。 - Sammy

0

使用FrameLayout进行包装。尝试使用以下XML代码:

详细解释请点击这里

<?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>


我将根布局更改为FrameLayout,但仍然不好。@Zeeshan - Sammy

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接