说明:
在正常的用fragment作为数据源应用于viewPager中,是不可以直接在viewPager中插入include自定义标题的,
如果想要插入自定义标题并且让标题随着Fragment同步滑动,在Fragment的xml代码中执行以下步骤即可:
1.将包含Fragment元素的准内容页面content.xml的布局改为线性布局LinerLayout,竖直排列
2.在ViewPager的上方在插入一个LinerLayout的线性布局(这是固定不动的标题栏),
3.将include自定义标题栏放入ViewPager上方的LinerLayout布局中,
4.将每一个Fragment元素的xml文件页面也改为线性布局,
在布局文件上方插入用LinerLayout包含的include自定义标题文件
到此就可以实现自定义标题随着Fragment同步滚动的效果
代码如下:
主内容页面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.work.suishenji.acvitity.ContentActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<include layout="@layout/title_layout"></include>
</LinearLayout>//此种标题固定不会改变
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
每一个Fragment.xml文件
<LinearLayout 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"
android:id="@+id/fg_jsb"
tools:context="com.work.suishenji.fragment.JsbFragment"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<include layout="@layout/title_layout"></include>
</LinearLayout>//这是可以随着fragent一起滚动的的标题栏
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_frgjsb"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>