0.效果图:
1.导入依赖:
implementation 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
2.在Activity或Fragment中的xml布局如下:
<com.viewpagerindicator.TabPageIndicator
android:id="@+id/indicator"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#1173FF"
/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
3.代码:
ArrayList<Fragment> fragments;
//声明pst的标题
String[] titles = {"全部","已完成","待完成","快超期","已超期","快手奖","蜗牛奖","Top10排行"};
@Override
protected int getLayoutId() {
return R.layout.msg_fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void initView(View view) {
fragments = new ArrayList<>();
IndexFragment1 indexFragment1 = new IndexFragment1();
IndexFragment2 indexFragment2 = new IndexFragment2();
IndexFragment3 indexFragment3 = new IndexFragment3();
IndexFragment4 indexFragment4 = new IndexFragment4();
IndexFragment5 indexFragment5 = new IndexFragment5();
IndexFragment6 indexFragment6 = new IndexFragment6();
IndexFragment7 indexFragment7 = new IndexFragment7();
IndexFragment8 indexFragment8 = new IndexFragment8();
//添加fragment到集合中时注意顺序
fragments.add(indexFragment1);
fragments.add(indexFragment2);
fragments.add(indexFragment3);
fragments.add(indexFragment4);
fragments.add(indexFragment5);
fragments.add(indexFragment6);
fragments.add(indexFragment7);
fragments.add(indexFragment8);
FragmentPagerAdapter adapter = new GoogleMusicAdapter(getActivity().getSupportFragmentManager(),titles,fragments);
ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
pager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator) view.findViewById(R.id.indicator);
indicator.setViewPager(pager);
indicator.setScrollBarSize(40);
}
@Override
public void initData() {
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
/**
* 自定义适配器
*/
class GoogleMusicAdapter extends FragmentPagerAdapter {
private String[] titles;
ArrayList<Fragment> fragments;
public GoogleMusicAdapter(FragmentManager fm, String[] titles, ArrayList<Fragment> fragments) {
super(fm);
this.titles = titles;
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
public CharSequence getPageTitle(int position) {
return titles[position];
}
@Override
public int getCount() {
return fragments.size();
}
}
4.
5.
除红色字体外是popMenu整合,不要删除即可