Android - Creating Multiple Levels of Tabs for ActionBar Using ViewPager and FragmentStatePagerAdapter

前端 未结 3 1420
闹比i
闹比i 2020-12-18 16:14

How can I handle Android tabs in an ActionBar such that it presents multiple contextual levels of tabs and associated fragments to the user (in other words, I want the Actio

3条回答
  •  春和景丽
    2020-12-18 16:45

    Finally, this is the HomeActivity with HomeFragment code that initializes and displays it all... all you have to do is create the appContext (find this simple solution on StackOverflow) and the remaining fragments, which you should rename to be meaningful to your solution...

    public class HomeActivity extends ActionBarActivity implements
            Level1_2Fragment.OnFragmentInteractionListener,
            Level1_3Fragment.OnFragmentInteractionListener,
            Level1_4Fragment.OnFragmentInteractionListener,
            Level1_2_1Fragment.OnFragmentInteractionListener,
            Level1_2_2Fragment.OnFragmentInteractionListener,
            Level1_2_3Fragment.OnFragmentInteractionListener,
            Level1_3_1Fragment.OnFragmentInteractionListener,
            Level1_3_1_1Fragment.OnFragmentInteractionListener,
            Level1_3_2Fragment.OnFragmentInteractionListener,
            Level1_3_2_1Fragment.OnFragmentInteractionListener,
            Level1_3_3Fragment.OnFragmentInteractionListener,
            Level1_3_3_1Fragment.OnFragmentInteractionListener,
            Level1_4_1Fragment.OnFragmentInteractionListener,
            Level1_4_2Fragment.OnFragmentInteractionListener
    {
        ABTabsAdapter mABTabsAdapter = null;
        ActionBar mActionBar = null;
        ViewPager mViewPager = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
    
            mActionBar = getSupportActionBar();
            mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
            mViewPager = new ViewPager(this);
            mViewPager.setId(R.id.pager_home);
            setContentView(mViewPager);
    
            mABTabsAdapter = new ABTabsAdapter(this, getSupportFragmentManager(), mViewPager, mActionBar);
    
            if (savedInstanceState != null)
            {
                //mActionBar.setSelectedNavigationItem(savedInstanceState.getInt("currentTab", 0));
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            getMenuInflater().inflate(R.menu.home, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item)
        {
            int id = item.getItemId();
            if (id == R.id.action_settings)
            {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    
        @Override
        protected void onSaveInstanceState(Bundle outState)
        {
            super.onSaveInstanceState(outState);
            outState.putInt("currentTab", mActionBar.getSelectedNavigationIndex());
        }
    
        public void onFragmentInteraction()
        {
        }
    
        public static class HomeFragment extends Fragment
        {
            public static HomeFragment newInstance()
            {
                HomeFragment fragment = new HomeFragment();
                return fragment;
            }
    
            public HomeFragment()
            {
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                View rootView = inflater.inflate(R.layout.fragment_home, container, false);
                return rootView;
            }
        }
    }
    

提交回复
热议问题