Android导航Tab栏实现
前言
Navigation Tabs
public class MainUIActivity extends FragmentActivity { private static final int CACHE_PAGER = 5; private ViewPager mViewPager; private MainUIPagerAdapter mainUIPagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mViewPager = new ViewPager(this); mViewPager.setId(11); setContentView(mViewPager); buildPhoneInterface(); } private void buildPhoneInterface() { buildViewPager(); buildActionBarAndViewPagerTitles(); getActionBar().setSelectedNavigationItem(1); } private void buildViewPager() { mViewPager = (ViewPager) findViewById(R.id.mainviewpager); mainUIPagerAdapter = new MainUIPagerAdapter(getFragmentManager()); mViewPager.setOffscreenPageLimit(CACHE_PAGER); mViewPager.setAdapter(mainUIPagerAdapter); mViewPager.setOnPageChangeListener(onPageChangeListener); } private void buildActionBarAndViewPagerTitles() { // 获取action bar的引用 ActionBar actionBar = getActionBar(); // 设置action bar的模式为导航模式,这样,action bar才会显示Tab标签 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // 只显示navigation tabs,不显示actionbar上面的标题等布局 actionBar.setDisplayShowTitleEnabled(false); actionBar.setDisplayUseLogoEnabled(false); actionBar.setDisplayShowHomeEnabled(false); // 构建一个自定义的adapter,将viewpager的切换和action bar tab的点击关联起来 MainTabListener mainTabListener = new MainTabListener(); actionBar.addTab(actionBar.newTab().setText(R.string.first).setTabListener(mainTabListener)); actionBar.addTab(actionBar.newTab().setText(R.string.second).setTabListener(mainTabListener)); actionBar.addTab(actionBar.newTab().setText(R.string.thrid).setTabListener(mainTabListener)); actionBar.addTab(actionBar.newTab().setText(R.string.fourth).setTabListener(mainTabListener)); } private Fragment getFirstFragment() { return getFragmentManager().findFragmentByTag(FirstFragment.class.getName()); } private Fragment getSecondFragment() { return getFragmentManager().findFragmentByTag(SecondFragment.class.getName()); } private Fragment getThirdFragment() { return getFragmentManager().findFragmentByTag(ThirdFragment.class.getName()); } private Fragment getFourthFragment() { return getFragmentManager().findFragmentByTag(FourthFragment.class.getName()); } // 构建一个简单的Viewpager切换监听类,将tab的点击与viewpager的切换关联起来 ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { getActionBar().setSelectedNavigationItem(position); } }; private class MainUIPagerAdapter extends AppFragmentPagerAdapter { List<Fragment> list = new ArrayList<Fragment>(); public MainUIPagerAdapter(FragmentManager fm) { super(fm); if (getFirstFragment() == null) { list.add(new FirstFragment()); } else { list.add(getFirstFragment()); } if (getSecondFragment() == null) { list.add(new SecondFragment()); } else { list.add(getSecondFragment()); } if (getThirdFragment() == null) { list.add(new ThirdFragment()); } else { list.add(getThirdFragment()); } if (getFourthFragment() == null) { list.add(new FourthFragment()); } else { list.add(getFourthFragment()); } } @Override public Fragment getItem(int position) { return list.get(position); } @Override protected String getTag(int position) { List<String> tagsList = new ArrayList<String>(); tagsList.add(FirstFragment.class.getName()); tagsList.add(SecondFragment.class.getName()); tagsList.add(ThirdFragment.class.getName()); tagsList.add(FourthFragment.class.getName()); return tagsList.get(position); } @Override public int getCount() { return list.size(); } } private class MainTabListener implements ActionBar.TabListener { @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { if (mViewPager.getCurrentItem() != tab.getPosition()) { mViewPager.setCurrentItem(tab.getPosition()); } } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。