自从google的design出来之后,很多控件效果实现起来已经不需要再像以前那样写自定义控件了,最近有个需求是要到页面头部标题栏的折叠效果,因此,想想用CollapsingToolbarLayout
CollapsingToolbarLayout主要是提供一个可折叠的Toolbar容器,对容器中的不同View设置layout_collapseMode折叠模式,来达到不同的折叠效果。通常CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout的搭配使用折叠的效果目前已经很常见了,在网上有很多的例子和文章,小编整理了一下知识点。
这个标志大家都很熟悉,滚动属性,主要有五个值
折叠模式,有两个值
<?xml version="1.0" encoding="utf-8"?>
<com.xxxx.views.ProgressLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/progress_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/ll_bottom_layout">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="150dp"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="@color/base_color"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<ImageView
android:id="@+id/iv_header_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/bg_personal_homepage_head"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<RelativeLayout
android:id="@+id/rl_album_bg_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bg_personal_homepage_head"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7">
<TextView
android:id="@+id/tv_album_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/space_10"
android:layout_marginRight="@dimen/space_10"
android:layout_marginBottom="@dimen/space_10"
android:layout_above="@+id/tv_tag_1"
android:textColor="@color/white"
android:textSize="@dimen/font_size_16"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_tag_1"
android:layout_width="wrap_content"
android:layout_height="@dimen/space_20"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/space_15"
android:layout_marginLeft="@dimen/space_10"
android:background="@drawable/bg_white_album_tag"
android:gravity="center"
android:textColor="@color/white"
android:textSize="@dimen/font_size_11" />
<TextView
android:id="@+id/tv_tag_2"
android:layout_width="wrap_content"
android:layout_height="@dimen/space_20"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/space_15"
android:layout_marginLeft="@dimen/space_10"
android:layout_toRightOf="@+id/tv_tag_1"
android:background="@drawable/bg_white_album_tag"
android:gravity="center"
android:textColor="@color/white"
android:textSize="@dimen/font_size_11" />
<TextView
android:id="@+id/tv_tag_3"
android:layout_width="wrap_content"
android:layout_height="@dimen/space_20"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/space_15"
android:layout_marginLeft="@dimen/space_10"
android:layout_toRightOf="@+id/tv_tag_2"
android:background="@drawable/bg_white_album_tag"
android:gravity="center"
android:textColor="@color/white"
android:textSize="@dimen/font_size_11" />
<TextView
android:id="@+id/tv_browse_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="@dimen/space_20"
android:layout_marginRight="@dimen/space_10"
android:drawableLeft="@drawable/icon_album_detail_browse"
android:drawablePadding="@dimen/space_5"
android:textColor="@color/white"
android:textSize="@dimen/font_size_11" />
</RelativeLayout>
<!-- 这边必须使用ToolBar -->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="48dp"
app:contentInsetStart="0dp"
app:contentScrim="@color/base_color"
app:layout_collapseMode="pin"
app:theme="@style/ToolbarTheme">
<com.xxxx.views.TitleBar
android:id="@+id/action_topbar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/transparent" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<com.xxxx.views.MySwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<com.xxxx.views.LoadMoreRecyclerView
android:id="@+id/lv_gamelist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clipToPadding="false"
android:overScrollMode="never"
android:scrollbars="none" />
</com.xxxx.views.MySwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
<LinearLayout
android:id="@+id/ll_bottom_layout"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:background="@color/white">
<RelativeLayout
android:id="@+id/rl_album_support"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/white_item_selector">
<TextView
android:id="@+id/tv_album_support"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:drawableLeft="@drawable/icon_album_flower"
android:drawablePadding="@dimen/space_8"
android:gravity="center"
android:textColor="@color/font_gray"
android:textSize="@dimen/font_size_11" />
</RelativeLayout>
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:layout_marginBottom="9dp"
android:layout_marginTop="9dp"
android:background="@color/album_line_color" />
<RelativeLayout
android:id="@+id/rl_album_collect"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/white_item_selector">
<TextView
android:id="@+id/tv_album_collect"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:drawableLeft="@drawable/icon_collect_selector"
android:drawablePadding="@dimen/space_8"
android:gravity="center"
android:textColor="@color/font_gray"
android:textSize="@dimen/font_size_11" />
</RelativeLayout>
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:layout_marginBottom="9dp"
android:layout_marginTop="9dp"
android:background="@color/album_line_color" />
<RelativeLayout
android:id="@+id/rl_album_comment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/white_item_selector">
<TextView
android:id="@+id/tv_album_comment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:drawableLeft="@drawable/icon_album_detail_comment"
android:drawablePadding="@dimen/space_8"
android:gravity="center"
android:textColor="@color/font_gray"
android:textSize="@dimen/font_size_11" />
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_alignParentBottom="true"
android:layout_marginBottom="48dp"
android:background="@color/album_detail_line" />
</com.xxxx.views.ProgressLayout>
appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
@Override
public void onStateChanged(AppBarLayout appBarLayout, State state) {
if( state == AppBarStateChangeListener.State.EXPANDED ) {
//展开状态
mTopBar.setTitle("");
}else if(state == State.COLLAPSED){
//折叠状态
mTopBar.setTitle(mAlbumName);
}else {
//中间状态
mTopBar.setTitle("");
}
}
});
import android.support.design.widget.AppBarLayout;
public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {
private State mCurrentState = State.IDLE;
@Override
public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
if (i == 0) {
if (mCurrentState != State.EXPANDED) {
onStateChanged(appBarLayout, State.EXPANDED);
}
mCurrentState = State.EXPANDED;
} else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
if (mCurrentState != State.COLLAPSED) {
onStateChanged(appBarLayout, State.COLLAPSED);
}
mCurrentState = State.COLLAPSED;
} else {
if (mCurrentState != State.IDLE) {
onStateChanged(appBarLayout, State.IDLE);
}
mCurrentState = State.IDLE;
}
}
public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
public static enum State {
EXPANDED,
COLLAPSED,
IDLE
}
}