android:collapsingtoolbar不工作

时间:2016-06-21 11:12:20

标签: android android-toolbar android-collapsingtoolbarlayout

我正在尝试使用以下代码折叠自定义工具栏:

<?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"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    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">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/white"
                app:contentInsetEnd="0dp"
                app:contentInsetLeft="0dp"
                app:contentInsetRight="0dp"
                app:contentInsetStart="0dp"
                app:layout_collapseMode="pin">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <LinearLayout
                        android:id="@+id/ll_custom_toolbar_normal"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:background="@color/colorPrimary"
                        android:gravity="center_vertical"
                        android:orientation="horizontal">

                        <ImageView
                            android:id="@+id/iv_toolbar_home"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:padding="@dimen/padding_16" />

                        <com.idolbee.android.customview.CustomTextView
                            android:id="@+id/tv_toolbar_title"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:gravity="center"
                            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                            android:textColor="@android:color/white"
                            custom:font="ars_maquette_pro_bold"
                            tools:text="Title" />

                        <ImageView
                            android:id="@+id/iv_toolbar_menu1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:paddingBottom="@dimen/padding_16"
                            android:paddingLeft="@dimen/padding_16"
                            android:paddingTop="@dimen/padding_16"
                            android:visibility="gone" />

                        <ImageView
                            android:id="@+id/iv_toolbar_menu"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:padding="@dimen/padding_16" />

                    </LinearLayout>

                    <ImageView
                        android:id="@+id/iv_my_stats_profile_pic"
                        android:layout_width="64dp"
                        android:layout_height="64dp"
                        android:layout_marginTop="@dimen/padding_16"
                        android:src="@drawable/profile_pic_holder" />

                    <com.idolbee.android.customview.CustomTextView
                        android:id="@+id/tv_my_stats_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="@dimen/margin_16"
                        android:layout_marginTop="@dimen/margin_16"
                        custom:font="ars_maquette_pro_bold"
                        tools:text="Isabel Lozano" />

                </LinearLayout>

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>


        <android.support.design.widget.TabLayout
            android:id="@+id/tl_my_stats"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            android:background="@color/colorAccent"
            app:layout_collapseMode="pin"
            custom:tabBackground="@color/colorAccent"
            custom:tabIndicatorColor="@color/colorPrimary"
            custom:tabMode="scrollable"
            custom:tabSelectedTextColor="@color/colorPrimary"
            custom:tabTextColor="@color/colorPrimary" />

    </android.support.design.widget.AppBarLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_my_stats"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:listitem="@layout/list_item_my_stats" />

    <RelativeLayout
        android:id="@+id/rl_fab_overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/actionbar_overlay"
        android:visibility="gone">

        <LinearLayout
            android:id="@+id/ll_home_fab_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="@dimen/margin_floating_action_menu"
            android:layout_marginRight="@dimen/margin_16"
            android:gravity="right"
            android:orientation="vertical">

        </LinearLayout>

    </RelativeLayout>

    <ImageView
        android:id="@+id/fab_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="@dimen/margin_16"
        android:src="@drawable/floating_btn_menu" />

</android.support.design.widget.CoordinatorLayout>

但它不起作用。 ToolBar不会与RecyclerView的滚动事件折叠。

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:0)

只需从 app:layout_scrollFlags 中删除 exitUntilCollapsed ,然后CollapsingToolBar就会开始工作。

PS:经过测试