正文
我们在进行开发的时候可能需要使用Fragment,更多时候是结合Tablayout和ViewPager来使用,我们的使用步骤一般是
- 获取Tablayout和ViewPager控件的实例
- 初始化各个Fragment
- 初始化自定义的FragmentPagerAdapter
- 给ViewPager设置适配器,即设置FragmentPagerAdapter
- 让Tablayout和ViewPager绑定
下面就让我们来看看具体怎么做
XML布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"