CoordinatorLayout + CollapsingToolbarLayout 向上滚动时不设置工具栏

CoordinatorLayout + CollapsingToolbarLayout not setting toolbar when scroll up

编辑: 向上滚动时我什至没有看到工具栏。它与建议的问题不重复。

CoordinatorLayout 中向上滚动时,我无法将 ImageViewTextView 放入工具栏。当我在 CoordinatorLayout 中向上滚动时,工具栏不可见。

   <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:fitsSystemWindows="false"
        android:clipToPadding="false"
        android:background="?android:attr/colorBackground"
        android:id="@+id/viewuserProfileMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">

            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:expandedTitleMarginStart="48dp"
                app:expandedTitleMarginEnd="64dp"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways">
                <RelativeLayout
                    android:id="@+id/relativeParent"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <ImageView
                        android:id="@+id/upCoverPhoto"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@color/redName"/>


                    <RelativeLayout
                        android:id="@+id/upDetails"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/diagonal_cut_layerlist"
                        android:elevation="12dp"
                        android:translationZ="12dp"
                        android:layout_marginBottom="20dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginTop="120dp">


                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="115dp"
                            android:layout_marginRight="10dp"
                            android:gravity="end"
                            android:orientation="horizontal">
                            <Button
                                android:id="@+id/Button_FollowFriends"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                style="@style/AppButton"
                                android:text="Follow"
                                />

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

                            <de.hdodenhof.circleimageview.CircleImageView
                                android:id="@+id/profilePicture_Cut"
                                android:layout_width="80dp"
                                android:layout_height="80dp"
                                android:src="@drawable/baby"
                                app:layout_scrollFlags="scroll|enterAlways"
                                app:civ_border_width="2dp"
                                app:layout_collapseMode="parallax"
                                app:layout_collapseParallaxMultiplier="0.7"
                                app:civ_border_color="@color/primary"
                                android:layout_marginTop="85dp"
                                android:layout_marginLeft="15dp"/>
                            <TextView
                                android:id="@+id/upUserName"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:elevation="12dp"
                                android:translationZ="12dp"
                                android:textColor="@color/secondary_text"
                                android:layout_marginTop="10dp"
                                android:textSize="28sp"
                                android:layout_marginLeft="10dp"
                                android:text="Susan Ledger"/>

                            <TextView
                                android:id="@+id/upUserCity"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:drawableLeft="@drawable/home"
                                android:drawablePadding="5dp"
                                android:layout_marginTop="5dp"
                                android:layout_marginLeft="10dp"
                                android:textSize="18sp"
                                android:textColor="@color/secondary_text"
                                android:text="Delhi,India"/>

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="25dp"
                                android:gravity="center"
                                android:orientation="horizontal"
                                android:weightSum="3">
                                <Button
                                    android:id="@+id/Button_AddFriend"
                                    android:layout_width="0dp"
                                    android:elevation="10dp"
                                    android:translationZ="10dp"
                                    android:layout_height="wrap_content"
                                    style="@style/AppButton"
                                    android:text="Add Friend"
                                    android:layout_weight="1"/>

                                <Button
                                    android:id="@+id/Button_RejectRequest"
                                    android:layout_width="0dp"
                                    android:layout_height="wrap_content"
                                    style="@style/AppButton"
                                    android:visibility="gone"
                                    android:text="Reject Request"
                                    android:layout_weight="1"/>

                                <Button
                                    android:id="@+id/Button_MessageFriend"
                                    android:layout_width="0dp"
                                    android:layout_height="wrap_content"
                                    style="@style/AppButton"
                                    android:text="Message"
                                    android:layout_weight="1"/>

                            </LinearLayout>
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="30dp"
                                android:gravity="center"
                                android:orientation="horizontal">

                                <LinearLayout
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:padding="8dp"
                                    android:orientation="vertical"
                                    android:gravity="center">

                                    <TextView
                                        android:id="@+id/FollowersCount"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:textStyle="bold"
                                        android:textColor="@color/primary"
                                        android:textSize="25sp"
                                        />

                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:textStyle="bold"
                                        android:textSize="20sp"
                                        android:text="follower"/>

                                </LinearLayout>

                                <LinearLayout
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:padding="10dp"
                                    android:orientation="vertical"
                                    android:gravity="center">

                                    <TextView
                                        android:id="@+id/FollowingCount"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:textStyle="bold"
                                        android:textColor="@color/primary"
                                        android:textSize="25sp"
                                        />

                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:textStyle="bold"
                                        android:textSize="20sp"
                                        android:text="following"/>


                                </LinearLayout>

                                <LinearLayout
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:padding="10dp"
                                    android:orientation="vertical"
                                    android:gravity="center">

                                    <TextView
                                        android:id="@+id/FriendsCount"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:textStyle="bold"
                                        android:textColor="@color/primary"
                                        android:textSize="25sp"
                                        />

                                    <TextView
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:textStyle="bold"
                                        android:textSize="20sp"
                                        android:text="friends"/>


                                </LinearLayout>

                            </LinearLayout>

                        </LinearLayout>



                    </RelativeLayout>
                </RelativeLayout>
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin" >
                </android.support.v7.widget.Toolbar>
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>


        <android.support.v4.widget.NestedScrollView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/up_NestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">



            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                >

                <!--To show tab on top of view pager-->
                <android.support.design.widget.TabLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabMode="scrollable"
                    app:tabTextColor="@color/place_autocomplete_prediction_primary_text_highlight"
                    app:tabSelectedTextColor="@color/primary_text"
                    app:tabIndicatorColor="@color/primary_light"
                    android:id="@+id/up_Viewpager_Tab">
                </android.support.design.widget.TabLayout>
                <android.support.v4.view.ViewPager
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@id/up_Viewpager_Tab"
                    android:id="@+id/up_Viewpager_ViewPager">

                </android.support.v4.view.ViewPager>    

            </RelativeLayout>

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

在片段中我正在设置标志以显示透明状态栏

 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public void themeNavAndStatusBar(Activity activity)
    {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
            return;

        Window w = activity.getWindow();
        w.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        w.setFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        w.setFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        w.setNavigationBarColor(activity.getResources().getColor(android.R.color.transparent));

        w.setStatusBarColor(activity.getResources().getColor(android.R.color.transparent));
    }

您是否尝试过将背景和标题更改为工具栏?检查它在您的布局中的可见位置。

试试这个

 <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar1"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:title="Toolbar"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay" >

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

这就是我得到的

已更新

在我的第一个评论中拿一个参考项目,并根据您的需要进行调整。找到下面的代码将帮助你

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="RtlHardcoded"
>

<android.support.design.widget.AppBarLayout
    android:id="@+id/main.appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    >

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/main.collapsing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"

        >

        <ImageView
            android:id="@+id/main.imageview.placeholder"
            android:layout_width="match_parent"
            android:layout_height="450dp"
            android:scaleType="centerCrop"
            android:src="@drawable/pattern"
            android:tint="#11000000"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.9"
            />

        <FrameLayout
            android:id="@+id/main.framelayout.title"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_gravity="bottom"
            android:orientation="vertical"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.3"
            >

            <LinearLayout
                android:id="@+id/main.linearlayout.title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:layout_gravity="center"
                android:orientation="vertical"
                >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginStart="20dp"
                    android:layout_gravity="left"
                    android:gravity="bottom|center"
                    android:text="Susan Ledger"
                    android:textColor="@android:color/white"
                    android:textSize="30sp"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:layout_marginTop="4dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginStart="20dp"
                    android:drawableLeft="@drawable/ic_home_black_24dp"
                    android:drawableStart="@drawable/ic_home_black_24dp"
                    android:gravity="center"
                    android:text="Delhi,India"
                    android:textColor="@android:color/white"
                    />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:gravity="center"
                    android:orientation="horizontal"
                    android:weightSum="3">
                    <Button
                        android:id="@+id/Button_AddFriend"
                        android:layout_width="0dp"
                        android:elevation="10dp"
                        android:translationZ="10dp"
                        android:layout_height="wrap_content"
                        android:text="Add Friend"
                        android:layout_weight="1"/>

                    <Button
                        android:id="@+id/Button_RejectRequest"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:visibility="gone"
                        android:text="Reject Request"
                        android:layout_weight="1"/>

                    <Button
                        android:id="@+id/Button_MessageFriend"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:text="Message"
                        android:layout_weight="1"/>

                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:layout_marginBottom="20dp"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="8dp"
                        android:orientation="vertical"
                        android:gravity="center">

                        <TextView
                            android:id="@+id/FollowersCount"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textStyle="bold"
                            android:textColor="@android:color/white"
                            android:textSize="25sp"
                            />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textStyle="bold"
                            android:textSize="20sp"
                            android:textColor="@android:color/white"
                            android:text="follower"/>

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="10dp"
                        android:orientation="vertical"
                        android:gravity="center">

                        <TextView
                            android:id="@+id/FollowingCount"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textStyle="bold"
                            android:textColor="@color/colorPrimary"
                            android:textSize="25sp"
                            />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textStyle="bold"
                            android:textSize="20sp"
                            android:text="following"/>


                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="10dp"
                        android:orientation="vertical"
                        android:gravity="center">

                        <TextView
                            android:id="@+id/FriendsCount"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textStyle="bold"
                            android:textColor="@color/colorPrimary"
                            android:textSize="25sp"
                            />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textStyle="bold"
                            android:textSize="20sp"
                            android:text="friends"/>


                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>
        </FrameLayout>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>


<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    >

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        app:cardElevation="8dp"
        app:contentPadding="16dp"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingExtra="8dp"
            android:text="@string/lorem"
            android:textSize="18sp"
            />
    </android.support.v7.widget.CardView>


</android.support.v4.widget.NestedScrollView>

<android.support.v7.widget.Toolbar
    android:id="@+id/main.toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@android:color/transparent"
    app:layout_anchor="@id/main.framelayout.title"
    app:theme="@style/ThemeOverlay.AppCompat.Dark"
    app:title=""
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        >

        <Space
            android:layout_width="@dimen/image_final_width"
            android:layout_height="@dimen/image_final_width"
            />

        <TextView
            android:id="@+id/main.textview.title"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:gravity="center_vertical"
            android:layout_weight="1"
            android:text="Susan Ledger"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            />

        <Button
            android:id="@+id/Button_FollowFriends"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_marginEnd="10dp"
            android:layout_gravity="center_vertical"
            android:text="Follow"
            />

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

<de.hdodenhof.circleimageview.CircleImageView
    android:layout_width="@dimen/image_width"
    android:layout_height="@dimen/image_width"
    android:layout_gravity="left"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    android:src="@drawable/profile"
    app:civ_border_color="@color/colorAccent"
    app:civ_border_width="2dp"
    app:finalHeight="@dimen/image_final_width"
    app:finalYPosition="2dp"     
 app:layout_behavior="com.md.collapsingfullview.Activity.AvatarImageBehavior"
    app:startHeight="2dp"
    app:startToolbarPosition="2dp"
    app:startXPosition="2dp"
    />
</android.support.design.widget.CoordinatorLayout> 

将此代码添加到 MainActivity。

    private void handleToolbarTitleVisibility(float percentage) {
        if (percentage >= PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR) {

            if(!mIsTheTitleVisible) {
                startAlphaAnimation(mTitle, ALPHA_ANIMATIONS_DURATION, View.VISIBLE);
                mIsTheTitleVisible = true;
            }

        } else {

            if (mIsTheTitleVisible) {
                startAlphaAnimation(mTitle, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE);
                mIsTheTitleVisible = false;
            }
        }

        if(percentage >= 0.9f)
        {
            mToolbar.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.colorPrimary));
        }
        else
        {
            mToolbar.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),android.R.color.transparent));
        }
}

输出

开始时

滚动后