Previous fragment visible under the new fragment

后端 未结 7 1371
心在旅途
心在旅途 2021-02-05 04:10

I have a tab + ViewPager layout and in one of these tabs I have a list view. When I replace that list fragment upon the onclick I can still see the old fragment u

相关标签:
7条回答
  • 2021-02-05 04:19

    In the root view add clickable = true and add a background color(both in the xml) of the fragment replacing the current fragment . Its just a fix for workaround

    0 讨论(0)
  • 2021-02-05 04:23

    Add this to both fragment parent layout

    android:background="@android:color/white".

    The one which you are replacing and one which you will replace.

    0 讨论(0)
  • 2021-02-05 04:23

    Your fragments should be loaded in FrameLayout like this

        <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:windowBackground" />
    

    And your fragments should be added/loaded in this frameLayout by this function

        private fun switchFragment(
        fragment: Fragment,
        addToBackstack: Boolean
    ) {
        //check new fragment is alredy loaded currently, then return
        val myFragment =
            supportFragmentManager.fragments.lastOrNull()//return current visible fragment or null
        if (myFragment != null && fragment::class == myFragment::class) {
            return
        }
    
        val fragmentManager = supportFragmentManager
        val transaction = fragmentManager.beginTransaction()
        //transaction.add(R.id.frameLayout, fragment, fragment.javaClass.name)
        transaction.replace(
            R.id.frameLayout,
            fragment,
            fragment.javaClass.name
        )//using replace will make sure that the previous fragment won't be visible from new fragment
        if (addToBackstack) {
            transaction.addToBackStack(fragment.javaClass.name)
        }
        transaction.commit()
    }
    

    So initially, your first fragment should be loaded like this,

    switchFragment(HomeFragment(), false)
    

    and then other fragments when you select from bottom navigation view or navigation drawer, call this function like this

    switchFragment(MyProfileFragment(), true)
    
    0 讨论(0)
  • 2021-02-05 04:26

    Instead of R.id.container put id of fragment like this: ((ViewGroup)getView().getParent()).getId(). Actually it is not replacing the fragment but the previous layout i.e FrameLayout. It works for me and i hope it will work in your case also.

    0 讨论(0)
  • 2021-02-05 04:28

    Best code for it. clearly and not used another ram of device. on you Activity onCreate or before adding fragments add this code:

    for (Fragment fragment : getSupportFragmentManager().getFragments()) {
        if (fragment instanceof NavigationDrawerFragment) {
            continue;
        }
        else if (fragment != null) {
            getSupportFragmentManager().beginTransaction().remove(fragment).commit();
        }
    }
    

    Happy coding :)

    0 讨论(0)
  • 2021-02-05 04:30

    when need to remove all views from the parent view you need to call removeAllViews() at container in your onCreateView() method of your fragment.

    Here is the code:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
    container.removeAllViews(); // Inflate the layout for this fragment
     return inflater.inflate(R.layout.fragment_example, container, false); 
    }
    
    0 讨论(0)
提交回复
热议问题