ViewPagerIndicator作为一款分页指标小部件兼容ViewPager,封装上做得非常不错,目前已为众多知名应用所使用。
ViewPagerIndicator+viewpager实现如下效果:(注:不需要导入或引入啥包就可实现)
六个类就可实现上图效果
activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 android:orientation="vertical"
5 android:layout_width="match_parent"
6 android:layout_height="fill_parent">
7
8 <com.viewpagerindicator.TabPageIndicator
9 android:id="@+id/indicator"
10 android:layout_height="wrap_content"
11 android:layout_width="match_parent"
12 />
13 <android.support.v4.view.ViewPager
14 android:id="@+id/pager"
15 android:layout_width="fill_parent"
16 android:layout_height="0dp"
17 android:layout_weight="1"
18 />
19 </LinearLayout>
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 }
工具类代码代码太多未给出,直接下载即可。
CSDN下载:http://download.csdn.net/detail/dickyqie/9705872
GitHub下载:https://github.com/DickyQie/LayoutFramework/tree/ViewPagerTabIndicator
共同学习,写下你的评论
评论加载中...
作者其他优质文章