【开源】TabBarView

TabBarView

使用说明:

声明一个TabBarView ,同时将ViewPager传递给它:

1
2
3
4
5
6
7
8
9
10
11
12
13
LayoutInflater inflator =
        (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.custom_ab, null);
tabBarView = (TabBarView) v.findViewById(R.id.tab_bar);
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(v);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
tabBarView.setViewPager(mViewPager);

同时,要TabBarView起作用,需要一个这样的adapter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class SectionsPagerAdapter extends FragmentPagerAdapter implements IconTabProvider{
     private int[] tab_icons={R.drawable.ic_tab1,
             R.drawable.ic_tab2,
             R.drawable.ic_tab3,
     };
     public SectionsPagerAdapter(FragmentManager fm) {
         super(fm);
     }
     @Override
     public Fragment getItem(int position) {
         // getItem is called to instantiate the fragment for the given page.
         // Return a PlaceholderFragment (defined as a static inner class
         // below).
         return PlaceholderFragment.newInstance(position + 1);
     }
     @Override
     public int getCount() {
         // Show 3 total pages.
         return tab_icons.length;
     }
     @Override
     public int getPageIconResId(int position) {
         return tab_icons[position];
     }
     @Override
     public CharSequence getPageTitle(int position) {
         Locale l = Locale.getDefault();
         switch (position) {
         case 0:
             return getString(R.string.title_section1).toUpperCase(l);
         case 1:
             return getString(R.string.title_section2).toUpperCase(l);
         case 2:
             return getString(R.string.title_section3).toUpperCase(l);
         }
         return null;
     }
 }

posted on 2015-03-27 10:41  wasdchenhao  阅读(109)  评论(0)    收藏  举报

导航