Show/Hide Fragments and change the visibility attribute programmatically

后端 未结 2 1072
谎友^
谎友^ 2021-01-02 19:37

This is a 2 part problem. What I\'m having is a 3 Fragment layout where the 3\'rd Fragment(FragmentC) is added dynamically when the user taps a button found in another fragm

2条回答
  •  悲哀的现实
    2021-01-02 20:28

    Create the Instances of fragments and add instead of replace

            FragA  fa= new FragA();
            FragB  fb= new FragB();
            FragC  fc= new FragB();
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fragmnt_container, fa);
            fragmentTransaction.add(R.id.fragmnt_container, fb);
            fragmentTransaction.add(R.id.fragmnt_container, fc);
            fragmentTransaction.show(fa);
            fragmentTransaction.hide(fb);
            fragmentTransaction.hide(fc);
            fragmentTransaction.commit();
    

    suppose we want to to hide and show different fragments based on our action

        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.hide(fa);
        fragmentTransaction.show(fb);
        fragmentTransaction.hide(fc);
        fragmentTransaction.commit();
    

提交回复
热议问题