android-studio整合ViewPagerIndicator开发

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整合,不要删除即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kenadc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值