最近做了一个关于tabLayout的Demo,其中也涉及到了Fragment复用以及传值。
效果图
话不多说,直接上代码
首先需要导入TabLayout依赖
compile 'com.android.support:design:25.0.0'
网上还有另外一个版本的TabLayout的依赖,根据自己的需要选择就好
compile 'com.android.support:design:22.2.0'
主页面布局
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/mTb" android:layout_width="match_parent" android:layout_height="wrap_content" //选中时游标的颜色 app:tabIndicatorColor="@color/red" //选中时的字体颜色 app:tabSelectedTextColor="@color/red" //未选中时的字体颜色 app:tabTextColor="@color/gray"> </android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/mVp" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> </LinearLayout>公用fragment布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_first_fragment" android:layout_width="match_parent" android:layout_height="match_parent" tools