Android TabLayout和Fragment联动

    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    private PagerAdapter mPagerAdapter;
    private String[] titles = {"推荐","分类","排行","管理","我的"};
    private List fragments;

定义一个储存title的数组和fragment的集合,初始化相关控件。

private void initView() {
        mTabLayout = findViewById(R.id.TabLayout);
        mViewPager = findViewById(R.id.ViewPager);

        initViewPagerFragment();
    }

    private void initViewPagerFragment() {
        mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
        fragments = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            fragments.add(FragmentFactory.creatFragment(i));
        }
        mPagerAdapter.setTitles(titles);
        mPagerAdapter.setFragments(fragments);
        mViewPager.setAdapter(mPagerAdapter);
        //将TabLayout和ViewPager绑定
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        //mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }

将fragment添加到集合中,我这里用的是一个工厂类,一会下方会贴代码,也可以一个一个new出来。代码中最后是设置TabLayout的模式,现在写的是将文字压缩到一整条中,注释的模式为可滚动式的。

public class FragmentFactory {

    /**
     * 推荐
     */
    public static final int TAB_RECOMMEND = 0;
    /**
     * 分类
     */
    public static final int TAB_CATEGORY = 1;
    /**
     * 排行
     */
    public static final int TAB_TOP = 2;
    /**
     * 管理
     */
    public static final int TAB_APPMANAGER = 3;
    /**
     * 我的
     */
    public static final int TAB_MY = 4;

    private static Map mFragmentMap = new HashMap<>();

    public static BaseFragment creatFragment(int index){
        BaseFragment fragment = mFragmentMap.get(index);
        //如果之前没有创建,就创建新的
        if (fragment == null){
            switch (index){
                case TAB_RECOMMEND:
                    fragment = new RecommendFragment();
                    break;
                case TAB_CATEGORY:
                    fragment = new CategoryFragment();
                    break;
                case TAB_TOP:
                    fragment = new TopFragment();
                    break;
                case TAB_APPMANAGER:
                    fragment = new AppManagerFragment();
                    break;
                case TAB_MY:
                    fragment = new MyFragment();
                    break;
            }
            //把创建的fragment存起来
            mFragmentMap.put(index,fragment);
        }
        return fragment;
    }
}

下面是Activity的布局




    

    


给Tablayout自定义一个style


适配器代码:

public class PagerAdapter extends FragmentStatePagerAdapter {

    private String[] titles;

    private List fragments = null;

    public void setTitles(String[] titles) {
        this.titles = titles;
    }

    public void setFragments(List fragments) {
        this.fragments = fragments;
    }

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

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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

一个最基本的TabLayout和Fragment联动效果就做好了。

看到网上有篇文章讲的挺全,链接:奔跑吧李博

你可能感兴趣的:(Android TabLayout和Fragment联动)