ViewPager和TabLayout和Fragment的组合使用

- 编辑:小昭 -

ViewPager和TabLayout和Fragment的组合使用

添加TabLayout的关联包

compile 'com.android.support:design:26.0.0-alpha1'


关联tab和vpa
tab.setupWithViewPager(zyVp);
创建fragment的页面 new 出来 fm的集合和页面名称的String集合 添加进适配器 适配器写法
public class VpAdapter extends FragmentPagerAdapter {
    private ArrayList<Fragment> list;
    private ArrayList<String> wz;

    public VpAdapter(FragmentManager fm, ArrayList<Fragment> list, ArrayList<String> wz) {
            super(fm);
            this.list = list;
            this.wz=wz;
    }

    public VpAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return wz.get(position);
    }

你会喜欢下面的文章? You'll like the following article.