首先在相应的布局文件中加入控件:
<?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" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/tlTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" app:tabIndicatorColor="#ffffff" app:tabSelectedTextColor="#000000" app:tabTextColor="#ffffff" ></android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/vpTabPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></android.support.v4.view.ViewPager> </LinearLayout>
xmlns:app="http://schemas.android.com/apk/res-auto"
app:tabIndicatorColor="#ffffff" tab底部指示器的颜色 app:tabSelectedTextColor="#000000" tab被选中时的颜色 app:tabTextColor="#ffffff" tab默认的字体颜色
然后创建相应个数的fragment及布局,此处省略
接着创建继承FragmentPagerAdapter的适配器:
public class FindTabAdapter extends FragmentPagerAdapter { private List<Fragment> list_fragment;//fragment列表 private List<String> list_Title;//tab名的列表 public FindTabAdapter(FragmentManager fm,List<Fragment> list_fragment,List<String> list_Title) { super(fm); this.list_fragment = list_fragment; this.list_Title = list_Title; } @Override public Fragment getItem(int position) { return list_fragment.get(position); } @Override public int getCount() { return list_Title.size(); } /* 这个方法是用来显示tab上的名字 */ @Override public CharSequence getPageTitle(int position) { return list_Title.get(position % list_Title.size()); } }
接着:
public class FindActivity extends AppCompatActivity { private TabLayout tlTitle; //定义TabLayout private ViewPager vpTabPager; //定义viewPager private FindTabAdapter Adapter; //定义adapter private List<Fragment> list_fragment; //定义要装fragment的列表 private List<String> list_title; //tab名称列表 private Find_hotRecommendFragment hotRecommendFragment; //热门推荐fragment private Find_hotCollectionFragment hotCollectionFragment; //热门收藏fragment private Find_hotMonthFragment hotMonthFragment; //本月热榜fragment private Find_hotToday hotToday; //今日热榜fragment private void init(){ tlTitle = (TabLayout) findViewById(R.id.tlTitle); vpTabPager = (ViewPager) findViewById(R.id.vpTabPager); hotRecommendFragment = new Find_hotRecommendFragment(); hotCollectionFragment= new Find_hotCollectionFragment(); hotMonthFragment = new Find_hotMonthFragment(); hotToday = new Find_hotToday(); //将fragment装进列表中 list_fragment = new ArrayList<>(); list_fragment.add(hotRecommendFragment); list_fragment.add(hotCollectionFragment); list_fragment.add(hotMonthFragment); list_fragment.add(hotToday); //将名称加载tab名字列表,正常情况下,我们应该在values/arrays.xml中进行定义然后调用 list_title = new ArrayList<>(); list_title.add("热门推荐"); list_title.add("热门收藏"); list_title.add("本月热榜"); list_title.add("今日热榜"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_find); this.init(); //设置TabLayout的模式 tlTitle.setTabMode(TabLayout.MODE_FIXED); //为TabLayout添加tab名称 tlTitle.addTab(tlTitle.newTab().setText(list_title.get(0))); tlTitle.addTab(tlTitle.newTab().setText(list_title.get(1))); tlTitle.addTab(tlTitle.newTab().setText(list_title.get(2))); tlTitle.addTab(tlTitle.newTab().setText(list_title.get(3))); Adapter = new FindTabAdapter(this.getSupportFragmentManager(),list_fragment,list_title); vpTabPager.setAdapter(Adapter); //TabLayout加载viewpager tlTitle.setupWithViewPager(vpTabPager); } }代码完毕。