Assigning Fragments to tabs in the ActionBar with different orientations

前端 未结 6 2203
半阙折子戏
半阙折子戏 2020-12-23 18:23

I have 3 fragments and an activity. I want to enable tabs on the ActionBar and assign a Fragment to each of the 3 tabs. How do I hook that up cor

6条回答
  •  误落风尘
    2020-12-23 19:08

            @Override
            public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
                // Check if the fragment is already initialized
    
                if (mFragment == null) {
                    // If not, instantiate and add it to the activity
                   // Toast.makeText(getApplicationContext(),"TAb "+tab.getPosition(),Toast.LENGTH_LONG).show();
                    if(tab.getPosition() == 1) {
                       mFragment = new HolderFragment();
                        fragmentTransaction.add(android.R.id.content, mFragment, mTag);
                    }
                    else if(tab.getPosition() == 0)
                    {
                        mFragment = new FragmentKnowledge();
                        fragmentTransaction.add(android.R.id.c`enter code here`ontent, mFragment, mTag);
    
                    }
                    /*else
                    {
    
                         mFragment = Fragment.instantiate(mActivity, mClass.getName());
                        fragmentTransaction.add(android.R.id.content, mFragment, mTag);
                    }
    */
                } else {
                    // If it exists, simply attach it in order to show it
                    fragmentTransaction.attach(mFragment);
                }
            }
    

提交回复
热议问题