private TabLayout mTabLayout;
private ViewPager mViewPager;
private PagerAdapter mPagerAdapter;
private String[] titles = {"推荐","分类","排行","管理","我的"};
private List<Fragment> 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<Integer,BaseFragment> 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的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cy.net.appstore.Activity.MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/TabLayout"
android:layout_width="match_parent"
style="@style/MyTablayoutstyle"
android:layout_height="40dp"/>
<android.support.v4.view.ViewPager
android:id="@+id/ViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
给Tablayout自定义一个style
<style name="MyTablayoutstyle" parent="Base.Widget.Design.TabLayout">
<item name="tabBackground">@color/white</item> //背景颜色
<item name="tabIndicatorColor">@color/blue</item> //指示线的颜色
<item name="tabIndicatorHeight">2dp</item> //指示线的高度
<item name="tabSelectedTextColor">@color/blue</item> //文字选中颜色
<item name="android:textSize">15sp</item>
<item name="android:textColor">@color/text</item>
</style>
适配器代码:
public class PagerAdapter extends FragmentStatePagerAdapter {
private String[] titles;
private List<Fragment> fragments = null;
public void setTitles(String[] titles) {
this.titles = titles;
}
public void setFragments(List<Fragment> 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联动效果就做好了。
看到网上有篇文章讲的挺全,链接:奔跑吧李博
网友评论