// Create the tabs that make up the user interface. mTabLayout = (TabLayout) findViewById(R.id.tabs); finalinttabCount= UiDataModel.getUiDataModel().getTabCount(); finalbooleanshowTabLabel= getResources().getBoolean(R.bool.showTabLabel); finalbooleanshowTabHorizontally= getResources().getBoolean(R.bool.showTabHorizontally); for (inti=0; i < tabCount; i++) { final UiDataModel.TabtabModel= UiDataModel.getUiDataModel().getTab(i); final@StringResintlabelResId= tabModel.getLabelResId();
final TabLayout.Tabtab= mTabLayout.newTab() .setTag(tabModel) .setIcon(tabModel.getIconResId()) .setContentDescription(labelResId);
if (showTabLabel) { tab.setText(labelResId); tab.setCustomView(R.layout.tab_item);
// Bind the icon to the TextView. finalDrawableicon= tab.getIcon(); if (showTabHorizontally) { // Remove the icon so it doesn't affect the minimum TabLayout height. tab.setIcon(null); text.setCompoundDrawablesRelativeWithIntrinsicBounds(icon, null, null, null); } else { text.setCompoundDrawablesRelativeWithIntrinsicBounds(null, icon, null, null); } }
/** Identifies each of the primary tabs within the application. */ publicenumTab { ALARMS(AlarmClockFragment.class, R.drawable.ic_tab_alarm, R.string.menu_alarm), CLOCKS(ClockFragment.class, R.drawable.ic_tab_clock, R.string.menu_clock), TIMERS(TimerFragment.class, R.drawable.ic_tab_timer, R.string.menu_timer), STOPWATCH(StopwatchFragment.class, R.drawable.ic_tab_stopwatch, R.string.menu_stopwatch);
// Customize the view pager. mFragmentTabPagerAdapter = newFragmentTabPagerAdapter(this); mFragmentTabPager = (ViewPager) findViewById(R.id.desk_clock_pager); // Keep all four tabs to minimize jank. mFragmentTabPager.setOffscreenPageLimit(3); // Set Accessibility Delegate to null so view pager doesn't intercept movements and // prevent the fab from being selected. mFragmentTabPager.setAccessibilityDelegate(null); // Mirror changes made to the selected page of the view pager into UiDataModel. mFragmentTabPager.addOnPageChangeListener(newPageChangeWatcher()); mFragmentTabPager.setAdapter(mFragmentTabPagerAdapter);
// Mirror changes made to the selected tab into UiDataModel. mTabLayout.addOnTabSelectedListener(newTabLayout.OnTabSelectedListener() { @Override publicvoidonTabSelected(TabLayout.Tab tab) { UiDataModel.getUiDataModel().setSelectedTab((UiDataModel.Tab) tab.getTag()); }
/** * This adapter produces the DeskClockFragments that are the content of the DeskClock tabs. The * adapter presents the tabs in LTR and RTL order depending on the text layout direction for the * current locale. To prevent issues when switching between LTR and RTL, fragments are registered * with the manager using position-independent tags, which is an important departure from * FragmentPagerAdapter. */ finalclassFragmentTabPagerAdapterextendsPagerAdapter {
privatefinal DeskClock mDeskClock;
/** The manager into which fragments are added. */ privatefinal FragmentManager mFragmentManager;
/** A fragment cache that can be accessed before {@link #instantiateItem} is called. */ privatefinal Map<UiDataModel.Tab, DeskClockFragment> mFragmentCache;
/** The active fragment transaction if one exists. */ private FragmentTransaction mCurrentTransaction;
/** The current fragment displayed to the user. */ private Fragment mCurrentPrimaryItem;
/** * @param position the left-to-right index of the fragment to be returned * @return the fragment displayed at the given {@code position} */ DeskClockFragment getDeskClockFragment(int position) { // Fetch the tab the UiDataModel reports for the position. final UiDataModel.Tabtab= UiDataModel.getUiDataModel().getTabAt(position);
// First check the local cache for the fragment. DeskClockFragmentfragment= mFragmentCache.get(tab); if (fragment != null) { return fragment; }
// Next check the fragment manager; relevant when app is rebuilt after locale changes // because this adapter will be new and mFragmentCache will be empty, but the fragment // manager will retain the Fragments built on original application launch. fragment = (DeskClockFragment) mFragmentManager.findFragmentByTag(tab.name()); if (fragment != null) { fragment.setFabContainer(mDeskClock); mFragmentCache.put(tab, fragment); return fragment; }
// Otherwise, build the fragment from scratch. finalStringfragmentClassName= tab.getFragmentClassName(); fragment = (DeskClockFragment) Fragment.instantiate(mDeskClock, fragmentClassName); fragment.setFabContainer(mDeskClock); mFragmentCache.put(tab, fragment); return fragment; }
@Override publicvoidstartUpdate(ViewGroup container) { if (container.getId() == View.NO_ID) { thrownewIllegalStateException("ViewPager with adapter " + this + " has no id"); } }
@Override public Object instantiateItem(ViewGroup container, int position) { if (mCurrentTransaction == null) { mCurrentTransaction = mFragmentManager.beginTransaction(); }
// Use the fragment located in the fragment manager if one exists. final UiDataModel.Tabtab= UiDataModel.getUiDataModel().getTabAt(position); Fragmentfragment= mFragmentManager.findFragmentByTag(tab.name()); if (fragment != null) { mCurrentTransaction.attach(fragment); } else { fragment = getDeskClockFragment(position); mCurrentTransaction.add(container.getId(), fragment, tab.name()); }
if (fragment != mCurrentPrimaryItem) { FragmentCompat.setMenuVisibility(fragment, false); FragmentCompat.setUserVisibleHint(fragment, false); }