在折叠栏布局android中向上滚动整个布局

问题描述 投票:8回答:2

下面是我的协调员布局的代码。它运作良好。

 <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:fitsSystemWindows="true">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="#f4f4f4"
            app:expandedTitleMarginEnd="16dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/header"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/echo3"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_alignParentBottom="true"
                    android:background="#fff">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:text="Private Albums"
                        android:textColor="#000"
                        android:textSize="22sp"
                        android:textStyle="bold" />
                </RelativeLayout>

            </RelativeLayout>

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

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/anim_toolbar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <include layout="@layout/content_profile" />


        <!--<fragment-->
        <!--android:id="@+id/detail"-->
        <!--android:name="<package>.<fragment_name>"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="match_parent" />-->

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

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

但我想淡化整个相对布局,因为我向上滚动它应该开始褪色(平均整个折叠栏布局)。它确实在特定点淡出,但我希望它在我向上滚动时淡出。谢谢,任何帮助表示赞赏。

android xml android-collapsingtoolbarlayout
2个回答
28
投票

通过将AppBarLayout.OnOffsetChangedListener连接到AppBarLayout,可以在向上滚动时减少RelativeLayout的alpha值。以下是我在我的应用中使用的代码。

appBar = (AppBarLayout) findViewById(R.id.app_bar_layout);
appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

            relativeLayoutToFadeOut.setAlpha(1.0f - Math.abs(verticalOffset / (float)
                    appBarLayout.getTotalScrollRange()));


        }
    });

0
投票

请参考下面的代码,对我来说工作正常..

 <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"
        android:id="@+id/view_profile_parent_layout"
        android:background="@color/splash_bg_color"
        tools:context=".Activity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_barLayout"
            android:layout_width="match_parent"
            android:layout_height="@dimen/view_profile_profile_app_bar_height"
            android:elevation="@dimen/view_profile_app_bar_elevation"
            android:theme="@style/ThemeOverlay.AppCompat.Light">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_bar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:contentScrim="?attr/colorPrimary"
                app:titleEnabled="false"
                android:fitsSystemWindows="true"
                app:layout_scrollFlags="exitUntilCollapsed|scroll"
                app:title="Collapsing Toolbar">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="?attr/actionBarSize">

                <de.hdodenhof.circleimageview.CircleImageView
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/view_profile_imageView"
                    android:layout_width="@dimen/view_profile_profile_width"
                    android:layout_height="@dimen/view_profile_profile_height"
                    android:src="@drawable/ic_profile_icon"
                    android:layout_centerHorizontal="true"
                    app:civ_border_width="@dimen/view_profile_profile_border_width"
                    app:civ_border_color="@color/textPrimary"/>

                <TextView
                    android:id="@+id/view_profile_profile_name_textView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/view_profile_profile_name_margin_top"
                    android:layout_below="@id/view_profile_imageView"
                    android:fontFamily="@font/roboto_regular"
                    android:text=""
                    android:textSize="@dimen/view_profile_profile_name_text_size"
                    android:gravity="center"
                    android:textColor="@color/white"/>

                </RelativeLayout>

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    app:layout_collapseMode="pin"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:elevation="@dimen/login_btn_elevation"
                    app:popupTheme="@style/AppTheme.PopupOverlay"
                    app:contentInsetLeft="0dp"
                    app:contentInsetStart="0dp"
                    android:contentInsetStart="0dp"
                    android:contentInsetLeft="0dp">

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

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

        </android.support.design.widget.AppBarLayout>
© www.soinside.com 2019 - 2024. All rights reserved.