S
S
Sergey Semenko2016-12-11 17:35:15
Android
Sergey Semenko, 2016-12-11 17:35:15

How to correctly catch the CollapsingToolbarLayout event?

There is the following layout:

<android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:statusBarScrim="?attr/colorPrimaryDark">

            <com.daimajia.slider.library.SliderLayout
                android:id="@+id/slider"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@android:color/white"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"/>

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

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

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

I want to make the icons in the STATUS BAR dark when scrolling, at the moment of changing the transparency of the content from the CollapsingToolbarLayout.
Did it like this:
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener()
        {
            @Override
            @TargetApi(Build.VERSION_CODES.M)
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                int toolbarHeight = toolbar.getHeight();
                int appBarHeight = appBarLayout.getHeight();

                int flagLight = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                int flag = appBarLayout.getSystemUiVisibility();

                if (appBarHeight + verticalOffset > 2 * toolbarHeight) {
                    if ((flag ^ flagLight) == flagLight) {
                        appBarLayout.setSystemUiVisibility(flag | flagLight);
                    }
                    return;
                }

                if ((flag & flagLight) == flagLight) {
                    appBarLayout.setSystemUiVisibility(flag ^ flagLight);
                }
            }
        });

But this method does not work exactly, i.e. icons become dark earlier than necessary (see gif)
5ab1468e23324759a20fd2aebd7869e6.gif

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question