Search in sources :

Example 1 with IHeaderRecyclerView

use of com.koushikdutta.boilerplate.recyclerview.IHeaderRecyclerView in project boilerplate by koush.

the class ScrollingToolbarLayout method enableToolbarScrollOff.

public void enableToolbarScrollOff(final IHeaderRecyclerView headerRecyclerView, final Fragment fragment) {
    scrollOffEnabled = true;
    int extra;
    View paddingView;
    if (getChildCount() == 3)
        paddingView = getChildAt(0);
    else
        paddingView = getChildAt(getChildCount() - 1);
    if (paddingView.getLayoutParams().height > 0) {
        extra = paddingView.getLayoutParams().height;
    } else {
        // apparently this is the max size allowed
        final int SIZE_MAX = 1073741823;
        paddingView.measure(MeasureSpec.makeMeasureSpec(SIZE_MAX, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(SIZE_MAX, MeasureSpec.AT_MOST));
        extra = paddingView.getMeasuredHeight();
    }
    AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, extra);
    FrameLayout frameLayout = new FrameLayout(getContext());
    frameLayout.setLayoutParams(lp);
    headerRecyclerView.addHeaderView(0, frameLayout);
    headerRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(RecyclerView absListView, int scrollState) {
            // when scrolling stops, and the toolbar is only partially scrolled off, force to scroll bar in completely
            if (scrollState != RecyclerView.SCROLL_STATE_IDLE)
                return;
            if (absListView.getChildCount() < 1)
                return;
            int firstVisibleItem = headerRecyclerView.findFirstVisibleItemPosition();
            if (firstVisibleItem != 0)
                return;
            final View toolbarContainer = getChildAt(getChildCount() - 1);
            if (toolbarContainer.getTranslationY() <= -toolbarContainer.getHeight() || (existingToolbarYAnimation != null && existingToolbarYEnd <= toolbarContainer.getHeight()))
                return;
            toolbarScrollIn();
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (recyclerView.getChildCount() < 1)
                return;
            if (fragment != null && !fragment.getUserVisibleHint())
                return;
            // cancelToolbarScroll();
            final View firstView = recyclerView.getChildAt(0);
            final View toolbarContainer = getChildAt(getChildCount() - 1);
            final View backdrop;
            if (getChildCount() == 3)
                backdrop = getChildAt(0);
            else
                backdrop = null;
            final int toolbarHeight = toolbarContainer.getHeight();
            int firstVisibleItem = headerRecyclerView.findFirstVisibleItemPosition();
            if (backdrop != null) {
                int newBackdropHeight;
                int backdropHeight = getResources().getDimensionPixelSize(R.dimen.icon_list_drawer_activity_backdrop_height);
                if (firstVisibleItem >= 1) {
                    newBackdropHeight = toolbarHeight;
                } else {
                    newBackdropHeight = firstView.getTop() + backdropHeight;
                }
                newBackdropHeight = Math.max(newBackdropHeight, toolbarHeight);
                FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) backdrop.getLayoutParams();
                lp.height = newBackdropHeight;
                // another option is to use y translation to not do parallax
                backdrop.setLayoutParams(lp);
                if (newBackdropHeight / (float) backdropHeight < .5f) {
                    toolbarFadeToPrimary();
                } else {
                    toolbarFadeToTranslucent();
                }
            }
            if (firstVisibleItem == 0) {
                int remainder = firstView.getHeight() + firstView.getTop();
                // if there's less than toolbar height left, start scrolling off.
                if (remainder < toolbarHeight) {
                    remainder = toolbarHeight - remainder;
                    if (existingToolbarYAnimation == null) {
                        float diff = -remainder - toolbarContainer.getTranslationY();
                        if (false && Math.abs(diff) > toolbarHeight / 4) {
                            if (toolbarContainer.getTranslationY() < -remainder) {
                                // scrolling down
                                toolbarScrollIn();
                            } else {
                                // scrolling up
                                toolbarScrollOut();
                            }
                        } else {
                            toolbarContainer.setTranslationY(-remainder);
                        }
                    }
                    if (backdrop != null)
                        backdrop.setTranslationY(-remainder);
                } else {
                    cancelToolbarScroll();
                    toolbarContainer.setTranslationY(0);
                    // toolbarScrollIn();
                    if (backdrop != null)
                        backdrop.setTranslationY(0);
                }
                return;
            }
            if (firstVisibleItem == 1) {
                cancelToolbarScroll();
                toolbarContainer.setTranslationY(-toolbarHeight);
            } else {
                toolbarScrollOut();
            }
            if (backdrop != null)
                backdrop.setTranslationY(-toolbarHeight);
        }
    });
    if (getChildCount() == 3)
        toolbarFadeToTranslucent();
}
Also used : FrameLayout(android.widget.FrameLayout) AbsListView(android.widget.AbsListView) RecyclerView(android.support.v7.widget.RecyclerView) IHeaderRecyclerView(com.koushikdutta.boilerplate.recyclerview.IHeaderRecyclerView) AbsListView(android.widget.AbsListView) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) IHeaderRecyclerView(com.koushikdutta.boilerplate.recyclerview.IHeaderRecyclerView)

Aggregations

RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 AbsListView (android.widget.AbsListView)1 FrameLayout (android.widget.FrameLayout)1 IHeaderRecyclerView (com.koushikdutta.boilerplate.recyclerview.IHeaderRecyclerView)1