实现步骤如下:
一:界面上写TabLayout与ViewPager两个控件
二:初始化数据(标题+Fragment)
三:添加Tab标签到TabLayout
四:设置适配器(ViewPager的适配器)
五:将TabLayout与ViewPager关联起来
一:界面上写TabLayout与ViewPager两个控件
代码如下:
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tbl_tablayout"
app:tabIndicatorColor="#F00"
app:tabSelectedTextColor="#F00"
app:tabTextColor="#1d1c1d"
app:tabMode="fixed">
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="115dp" />
其中:TabLayout中的可以设置一些变量
app:tabIndicatorColor="#F00"<!-- 下方滚动的下划线颜色 -->
app:tabSelectedTextColor="#F00" <!-- tab被选中后,文字的颜色 -->
app:tabTextColor="#1d1c1d"<!-- tab中字体的颜色 -->
app:tabIndicatorHeight="5dp"<!--指示条的高度-->
app:tabTextAppearance="@style/App_Theme"<!--可以改变tab标签的字体的大小-->
三:添加Tab标签到TabLayout
tblTablayout.addTab(tblTablayout.newTab().setText(titles.get(i)));
四:设置适配器(ViewPager的适配器)
在适配器中必须重写getPageTitle()方法要不然标题是不会显示出来的
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
五:将TabLayout与ViewPager关联起来
tblTablayout.setupWithViewPager(vpViewpager);
实例代码如下:
Activity:
public class TabLayoutActivity extends BaseActivity {
@BindView(R.id.tbl_tablayout)
TabLayout tblTablayout;
@BindView(R.id.vp_viewpager)
ViewPager vpViewpager;
private List<String> titles = new ArrayList<>();//放标题
private List<Fragment> fragments = new ArrayList<>();//放fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_layout);
ButterKnife.bind(this);
initView();
}
private void initView() {
//添加fragment
fragments.add(TabLayoutItemFragment.newInstance("111111", "111111"));
fragments.add(TabLayoutItemFragment.newInstance("222222", "222222"));
fragments.add(TabLayoutItemFragment.newInstance("333333", "333333"));
fragments.add(TabLayoutItemFragment.newInstance("444444", "444444"));
//添加标题
titles.add("fragment1");
titles.add("fragment2");
titles.add("fragment3");
titles.add("fragment4");
//添加tab标签
for (int i = 0; i < titles.size(); i++) {
tblTablayout.addTab(tblTablayout.newTab().setText(titles.get(i)));
}
//添加设置适配器
vpViewpager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
//把TabLayout与ViewPager关联起来
tblTablayout.setupWithViewPager(vpViewpager);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter{
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
//让标题显示出来
return titles.get(position);
}
}
}
xml:
<?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=".activity.TabLayoutActivity">
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tbl_tablayout"
app:tabIndicatorColor="#F00"
app:tabSelectedTextColor="#F00"
app:tabTextColor="#1d1c1d"
app:tabMode="fixed">
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="115dp" />
</LinearLayout>
希望能对大家实现这样的功能有所帮助