Fragment is not being replaced but put on top of t

2020-01-23 16:50发布

Activity:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

Fragment1 fragment = new Fragment1();
Fragment2 fragment2 = new Fragment2();

transaction.replace(R.id.Fragment1, fragment);
transaction.addToBackStack(null);
transaction.commit();

FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();
transaction2.replace(R.id.Fragment1, fragment2);
transaction2.addToBackStack(null);
transaction2.commit();

Code in the view:

<fragment
    android:id="@+id/Fragment1"
    android:name="com.landa.fragment.Fragment1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/include1" /> 

The problem is, the content doesn't really get replaced - it gets put on top (so it overlaps).

When I click back, the first fragment gets shown properly (without the second), but initially both are visible (I want only the last one to be visible).

What am I missing here?

15条回答
Anthone
2楼-- · 2020-01-23 17:06

I have tried above all the case and try to fix the issue but still not got solution. I don't know why its not working for me. I agree with above answers because so many agree with it.

I am just extending the answer and below is the way which is working for me.

I have added this property android:clickable="true" in top parent layout on fragment.xml file.

I hope someone will get help.

查看更多
一纸荒年 Trace。
3楼-- · 2020-01-23 17:08

Thank you to Sapan Diwakar and others. This answer helped me. I simply created a RelativeLayout without a fragment inside it, used the RelativeLayout as a parent or group view, specified that group view in the transaction to replace, and it worked.

Code: getSupportFragmentManager().beginTransaction() .replace(R.id.content_group_view, itemDetailFragment).commit();

Layout:

 <RelativeLayout
         android:id="@+id/content_group_view"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
 </RelativeLayout>
查看更多
萌系小妹纸
4楼-- · 2020-01-23 17:09

You can use setTransition method from FragmentTransaction to fix overlapping issue.

transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
查看更多
来,给爷笑一个
5楼-- · 2020-01-23 17:09

i had same problem after using this problem solved

  1. Take frame layout where u are replacing ur fragment

    <FrameLayout
                    android:id="@+id/fragment_place"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
  2. and code for replacement

    public void selectFrag(View view) {
    Fragment fr;
    
      if(view == findViewById(R.id.button2))
    {
         fr = new FragmentTwo();
    
     }else {
         fr = new FragmentOne();
     }
     FragmentManager fm = getFragmentManager();
     FragmentTransaction fragmentTransaction = fm.beginTransaction();
     fragmentTransaction.replace(R.id.fragment_place, fr);
     fragmentTransaction.commit();
    

if you want whole example i will send you just comment me......

查看更多
Luminary・发光体
6楼-- · 2020-01-23 17:12

In Oncreate ()

  BaseFragmentloginpage fragment = new BaseFragmentloginpage();
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame, fragment);
            fragmentTransaction.isAddToBackStackAllowed();
            fragmentTransaction.commit();

now this will be always your default fragment ...if you add fragment in xml layout it will always overlap , so set initial content with fragment at run time

查看更多
We Are One
7楼-- · 2020-01-23 17:13
<FrameLayout
    android:id="@+id/fragment_frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:windowBackground">
//Add fragment here
</FrameLayout>

Add android:background="?android:windowBackground"> to the container in which your fragment resides. In in my case I added a FrameLayout, that should help.

查看更多
登录 后发表回答