ViewPagerIndicator+viewpager的简单使用,不需要导入Library包
ViewPagerIndicator作为一款分页指标小部件兼容ViewPager,封装上做得非常不错,目前已为众多知名应用所使用。
ViewPagerIndicator+viewpager实现如下效果:(注:不需要导入或引入啥包就可实现)
六个类就可实现上图效果
MainActivity.java
1 public class MainActivity extends FragmentActivity { 2 private static final String[] CONTENT = new String[] { "推荐", "热点", "视频", "本地", "科技", "健康" }; 3 4 private List<Fragment> list=new ArrayList<Fragment>(); 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 10 FragmentPagerAdapter adapter = new GoogleMusicAdapter(getSupportFragmentManager()); 11 12 ViewPager pager = (ViewPager)findViewById(R.id.pager); 13 pager.setAdapter(adapter); 14 15 TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator); 16 indicator.setViewPager(pager); 17 } 18 19 class GoogleMusicAdapter extends FragmentPagerAdapter { 20 public GoogleMusicAdapter(FragmentManager fm) { 21 super(fm); 22 list.add(new TestFragment(CONTENT[0])); 23 list.add(new TestFragment(CONTENT[1])); 24 list.add(new TestFragment(CONTENT[2])); 25 list.add(new TestFragment(CONTENT[3])); 26 list.add(new TestFragment(CONTENT[4])); 27 list.add(new TestFragment(CONTENT[5])); 28 } 29 30 @Override 31 public Fragment getItem(int position) { 32 return list.get(position); 33 } 34 35 @Override 36 public CharSequence getPageTitle(int position) { 37 return CONTENT[position % CONTENT.length].toUpperCase(); 38 } 39 40 @Override 41 public int getCount() { 42 return CONTENT.length; 43 } 44 } 45 }
Fragment.Java
1 public final class TestFragment extends Fragment { 2 3 private String s; 4 5 public TestFragment(String s) 6 { 7 this.s=s; 8 } 9 10 @Override 11 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 12 13 return inflater.inflate(R.layout.fragment_news, container, false); 14 } 15 16 @Override 17 public void onActivityCreated( Bundle savedInstanceState) { 18 // TODO Auto-generated method stub 19 super.onActivityCreated(savedInstanceState); 20 View view=getView(); 21 TextView te=(TextView)view.findViewById(R.id.textView1); 22 te.setText(s); 23 } 24 25 public void initView() { 26 } 27 }
工具类代码代码太多未给出,直接下载即可。
源码点击下载:https://github.com/DickyQie/android-tab