Switching between Fragments in a single Activity

后端 未结 3 1968
清酒与你
清酒与你 2021-02-03 10:12

I want to create an Activity which shows a sort of menu a user can go through. By clicking an item, a new screen is shown, allowing the user more options (wizard-li

3条回答
  •  被撕碎了的回忆
    2021-02-03 11:00

    I create this main layout:

    
    
       
    
    
    

    And I replenished in the FrameActivity with:

    @Override
    public void onCreate(Bundle savedInstanceState) {
      ...
      Fragment fragment = new Dashboard();
      FragmentManager fm = getSupportFragmentManager();
      FragmentTransaction transaction = fm.beginTransaction();
      transaction.replace(R.id.contentFragment, fragment);
      transaction.commit();
      ...
    }
    

    And I repleace on onClick Method with the same code, changing Fragment (Dashboard for Events):

    @Override
    public void onClick.... {
      ...
      Fragment fragment = new Events();
      FragmentManager fm = getSupportFragmentManager();
      FragmentTransaction transaction = fm.beginTransaction();
      transaction.replace(R.id.contentFragment, fragment); //Container -> R.id.contentFragment
      transaction.commit();
      ...
    }
    

提交回复
热议问题