跟viewpagerindicator一样,是一个指示器,用着感觉比viewpagerindicator简单点,
用studio开发,只需要在gradlewenjian一个依赖
compile 'com.android.support:design:23.4.0'
这个有时候会需要根据自己的工程,适当修改然后就可以在工程中引用了
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:orientation="vertical">
- <android.support.design.widget.TabLayout
- android:id="@+id/tab_FindFragment_title"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/titleBlue"
- app:tabIndicatorColor="@color/white"
- app:tabSelectedTextColor="@color/gray"
- app:tabTextColor="@color/white"
- />
- <android.support.v4.view.ViewPager
- android:id="@+id/vp_FindFragment_pager"
- android:layout_width="fill_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- />
- </LinearLayout>
在这里,
- app:tabIndicatorColor="@color/white" // 下方滚动的下划线颜色
- app:tabSelectedTextColor="@color/gray" // tab被选中后,文字的颜色
- app:tabTextColor="@color/white" // tab默认的文字颜色
下面是viewPager的继承FragmentAdapter的一个适配器
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentPagerAdapter;
- import java.util.List;
- /**
- * Created by cg on 2015/9/26.
- */
- public class Find_tab_Adapter extends FragmentPagerAdapter {
- private List<Fragment> list_fragment; //fragment列表
- private List<String> list_Title; //tab名的列表
- public Find_tab_Adapter(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());
- }
- }
下面是activity里面的代码
TabLayout tabLayout= findViewById(R.id.tablayout);
//viewpager
ViewPager vp = findViewById(R.id.shouye_vp);
vp.setAdapter(new ShouYeVPAdapter(mActivity.getSupportFragmentManager()));
tabLayout.setupWithViewPager(vp);
tabLayout.setTabMode(TabLayout.MODE_FIXED);