Smooth animated Collapsing Toolbar with Android Design Support Library

metemet06 picture metemet06 · Jul 6, 2015 · Viewed 11.1k times · Source

Are there anyway to make Android Design Support Library's Collapsing animation smoother while scrolling? When I release scrolling, it stops suddenly. But what I want is: collapsing animation will continue smoothly even if you stop scrolling. Android-ObservableScrollView and Scrollable are the libraries that are collapsing smoothly.

Answer

Jossy Paul picture Jossy Paul · Dec 28, 2015

You can use the new layout_scrollFlag snap for smooth scroll within the AppBarLayout states. But what I have experienced is that, when the RecyclerView reaches top, scrolling stops. i.e CollapsingToolbarLayout won't get expanded without another scroll. For the RecyclerView to scroll smoothly up and expand the CollapsingToolbarLayout I have used a ScrollListener on recyclerview.

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        int scrollDy = 0;
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            scrollDy += dy;
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if(scrollDy==0&&(newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE))
            {
                AppBarLayout appBarLayout = ((AppBarLayout) view.findViewById(R.id.app_bar));

                appBarLayout.setExpanded(true);
            }
        }
    });

I used "scroll|exitUntilCollapsed" as layout_scrollFlags.

<android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:minHeight="80dp"
            app:layout_collapseMode="none"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">