<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="236dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolBarLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/course_bg"
app:contentScrim="@drawable/course_title_bg"
app:expandedTitleMarginTop="10dp"
app:collapsedTitleGravity="center"
app:expandedTitleGravity="center"
app:collapsedTitleTextAppearance="@style/course_title_style"
app:expandedTitleTextAppearance="@style/course_title_style"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:title="哈哈哈哈88哈哈00哈哈11哈哈">
<TextView
android:id="@+id/subTitle"
android:layout_width="282dp"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:layout_marginTop="60dp"
android:maxLines="2"
android:ellipsize="end"
android:gravity="center"
android:textColor="@android:color/white"
app:layout_collapseParallaxMultiplier="0.7"
android:textSize="17dp"
android:text="中啊哈哈哈哈88哈哈00哈哈11哈哈"
app:layout_collapseMode="parallax" />
<TextView
android:layout_marginTop="40dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="1"
android:textColor="#ffffffff"
android:textSize="12sp"
android:text="有效期:2019.05.04"
/>
<View
android:id="@+id/top_view"
android:layout_width="match_parent"
android:layout_height="0dp" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:layout_gravity="top"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:titleMarginStart="0dp"
android:paddingStart="0dp"
android:gravity="center"
app:title=""
app:titleTextAppearance="@style/course_title_style"
app:layout_collapseMode="pin">
<ImageView
android:id="@+id/title_back"
android:layout_width="65dp"
android:layout_height="18dp"
android:layout_centerInParent="true"
android:layout_gravity="left"
android:src="@drawable/icon_course_title_back"
android:paddingEnd="30dp"
android:layout_marginStart="9dp"/>
<LinearLayout
android:id="@+id/title_service"
android:layout_gravity="right"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_width="77dp"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/icon_course_more"
android:layout_width="15dp"
android:layout_height="13dp" />
<TextView
android:layout_marginLeft="5dp"
android:layout_marginRight="11dp"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13dp"
android:text="服務"
/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
app:tabIndicatorColor="@color/white"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/white"
app:tabTextAppearance="@style/DesignTabLayoutTextStyle"
app:tabTextColor="@color/color_999999"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="@+id/address_bottom_container"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:gravity="center_vertical"
android:layout_alignParentBottom="true"
android:paddingBottom="@dimen/x8"
android:paddingTop="@dimen/x8"
android:background="#FCFCEE"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:textSize="@dimen/text_size_13"
android:textColor="@color/color_2D2D2D"
android:layout_marginLeft="@dimen/x20"
android:layout_marginRight="@dimen/x40"
android:text="@string/tips_noaddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/btn_to_confirm"
android:text="@string/taocan_toconfirm"
android:background="@drawable/btn_taocan_quiry"
android:textColor="@color/white"
android:textSize="@dimen/text_size_15"
android:paddingLeft="@dimen/x15"
android:paddingRight="@dimen/x15"
android:paddingTop="@dimen/x8"
android:paddingBottom="@dimen/x8"
android:layout_marginRight="@dimen/x18"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
複製代碼
java代碼:java
定義
private CollapsingToolbarLayoutState state;
private TextView subTitle;
private CollapsingToolbarLayout toolbar;
private AppBarLayout app_bar;
private Toolbar mToolBar;
初始化
toolbar = findViewById(R.id.toolBarLayout);
app_bar = findViewById(R.id.appBar);
mToolBar = findViewById(R.id.toolbar);
subTitle = findViewById(R.id.subTitle);
設置事件
app_bar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (verticalOffset == 0) {
if (state != CollapsingToolbarLayoutState.EXPANDED) {//徹底拉下
state = CollapsingToolbarLayoutState.EXPANDED;//修改狀態標記爲展開
toolbar.setTitle("");//設置title爲EXPANDED
subTitle.setVisibility(View.VISIBLE);
}
} else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()-mToolBar.getMeasuredHeight()/3) {//徹底上去
if (state != CollapsingToolbarLayoutState.COLLAPSED) {
toolbar.setTitle(myCourseListItemBean.getName());//設置title不顯示
subTitle.setVisibility(View.GONE);
state = CollapsingToolbarLayoutState.COLLAPSED;//修改狀態標記爲摺疊
}
} else {
if (state != CollapsingToolbarLayoutState.INTERNEDIATE) {//上去一點
toolbar.setTitle("");//設置title爲INTERNEDIATE
subTitle.setVisibility(View.VISIBLE);
state = CollapsingToolbarLayoutState.INTERNEDIATE;//修改狀態標記爲中間
}
}
}
});
public enum CollapsingToolbarLayoutState {
EXPANDED,
COLLAPSED,
INTERNEDIATE
}
<style name="course_title_style">
<item name="android:textSize">17sp</item>
</style>
複製代碼