在app模块build.gradle添加TabLayout依赖
implementation 'com.android.support:design:27.1.0'
布局中添加TabLayout和ViewPager
<android.support.constraint.ConstraintLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:tabIndicatorColor="@color/colorAccent"
app:tabIndicatorHeight="2dp"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorAccent">
android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/tab"
app:layout_constraintBottom_toBottomOf="parent"
>
android.support.v4.view.ViewPager>
android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity {
private TabLayout mTabLayout;
private ViewPager mViewPager;
private ArrayList<String> mTitleList = new ArrayList<>();//页卡标题集合
private Fragment firstFragment,secondFragment;//页卡视图
private ArrayList<Fragment> mViewList = new ArrayList<>();//页卡视图集合
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = findViewById(R.id.viewpager);
mTabLayout = findViewById(R.id.tab);
firstFragment = new FirstFragment();
secondFragment = new SecondFragment();
//添加页卡视图
mViewList.add(firstFragment);
mViewList.add(secondFragment);
//添加页卡标题
mTitleList.add("First");
mTitleList.add("Second");
mTabLayout.setTabMode(TabLayout.MODE_FIXED);//设置tab模式,当前为系统默认模式
mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(0)));//添加tab选项卡
mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(1)));
}
}
mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
//获取每个页卡
@Override
public android.support.v4.app.Fragment getItem(int position) {
return mViewList.get(position);
}
//获取页卡数
@Override
public int getCount() {
return mTitleList.size();
}
//获取页卡标题
@Override
public CharSequence getPageTitle(int position) {
return mTitleList.get(position);
}
});
适配器中实现了三个方法
1.getItem:获取每个页卡
2.getCount:获取页卡数
3.getPageTitle:获取页卡标题
mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来。
public class MainActivity extends AppCompatActivity {
private TabLayout mTabLayout;
private ViewPager mViewPager;
private ArrayList<String> mTitleList = new ArrayList<>();//页卡标题集合
private Fragment firstFragment,secondFragment;//页卡视图
private ArrayList<Fragment> mViewList = new ArrayList<>();//页卡视图集合
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = findViewById(R.id.viewpager);
mTabLayout = findViewById(R.id.tab);
firstFragment = new FirstFragment();
secondFragment = new SecondFragment();
//添加页卡视图
mViewList.add(firstFragment);
mViewList.add(secondFragment);
//添加页卡标题
mTitleList.add("First");
mTitleList.add("Second");
mTabLayout.setTabMode(TabLayout.MODE_FIXED);//设置tab模式,当前为系统默认模式
mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(0)));//添加tab选项卡
mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList.get(1)));
mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
//获取每个页卡
@Override
public android.support.v4.app.Fragment getItem(int position) {
return mViewList.get(position);
}
//获取页卡数
@Override
public int getCount() {
return mTitleList.size();
}
//获取页卡标题
@Override
public CharSequence getPageTitle(int position) {
return mTitleList.get(position);
}
});
mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来。
}
}
FirstFragment布局文件
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".FirstFragment">
<TextView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Fragment" />
android.support.constraint.ConstraintLayout>
SecondFragment布局文件
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".SecondFragment">
<TextView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Fragment" />
android.support.constraint.ConstraintLayout>
FirstFragment界面代码
public class FirstFragment extends Fragment {
private View rootView;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_first, container, false);
initUi();
return rootView;
}
private void initUi() {
}
}
SecondFragment界面代码
public class SecondFragment extends Fragment {
private View rootView;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_second, container, false);
initUi();
return rootView;
}
private void initUi() {
}
}
https://github.com/wkxjc/ViewPagerStudy