How can I show a new Fragment within a single tab?

前端 未结 3 1318
灰色年华
灰色年华 2021-02-04 15:47

I am creating a three tabs that contains one fragment each now I want to replace the first Tab\'s Fragment with a new Fragment within the same Tab. How can I do that with the ta

3条回答
  •  春和景丽
    2021-02-04 15:54

    OK, several things:

    1. You don't need both replace and remove. Replace effectively just removes the first fragment and adds the second in its place.

    2. I suspect that your arguments are incorrect for the replace command. This is the proper form:

      replace (int containerViewId, Fragment fragment)
      

      You seem to be passing the id of the fragment itself, rather than the id of the container. The replace command takes the id of the container view and removes all fragments in that container before adding a new one. So if this was your layout:

      
      
          
      
      
      

      You would use:

      replace(R.id.container, mFragment);
      

      and not

      replace(R.id.main_fragment, mFragment);
      
    3. Since you are trying to findFragmentById(R.id.main_fragment), I suspect that you have added this fragment to your XML layout file. If you have a fragment added directly in the XML, you cannot remove it programmatically at run time. In order to remove or replace fragments, you must have added them with a FragmentTransaction at run time.

    EDIT:

    Numbers 2 and 3 from my original answer still apply. You need to use the id of the containing ViewGroup for adding and replacing. In this case, that's your LinearLayout, R.id.Maincontainer, not R.id.main_fragment.

    Also, as I said before, you cannot remove fragments that you have added directly in the XML. Your main_fragment is added in the XML and therefore cannot be removed at run time.

    Try this for your mainfragment.xml:

    
    
    
        

    And this for your main_fragment class:

    public class main_activity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mainfragment);
    
            //Notice that I am adding this 1st fragment in the Activity and not the XML
            main_fragment mainFragment = new main_fragment();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(R.id.Maincontainer, mainFragment);
            ft.commit();
    
            mButton = (Button) findViewById(R.id.btn);
            mButton.setOnClickListener(new OnClickListener()
            {
    
                @Override
                public void onClick(View v)
                {
    
                    Fragment mFragment = new third_fragment_view();
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    //Replacing using the id of the container and not the fragment itself
                    ft.replace(R.id.Maincontainer, mFragment);
                    ft.addToBackStack(null);
                    ft.commit();
                }
            });
        }
    }
    

提交回复
热议问题