Replace fragments in FrameLayout layout

前端 未结 4 1331
情话喂你
情话喂你 2020-12-17 08:29

I have tried every tutorial on the first google page about android fragments, but I can\'t get anything to work.

So I have one navigation bar activity, MainAct

4条回答
  •  时光取名叫无心
    2020-12-17 08:50

    Try this one. it is my newbie code easier to understand :)

    FragmentTransaction transaction;
    
    switch (id){
                case R.id.button_fragment_one:
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.frame_layout, new FragmentOne());
                        transaction.addToBackStack(null);
                        transaction.commit();
                    break;
                case R.id.button_fragment_two:
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.frame_layout, new FragmentTwo());
                        transaction.commit();
                    break;
                default:
                    break;
    }
    

提交回复
热议问题