Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Behaviour with Coordinator Layout

I am trying to hide and show a view when my recyclerview scrolls with the help of coordinator layout.

My view is a linearlayout with button and it is not a fab, toolbar or tablayout as I already know how to hide them on scroll.

Please note this is not a duplicate as all answers show how to fab toolbar or tablayout

This is xml i am using

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:id="@+id/prodMain"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <RelativeLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/gray_light"
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <LinearLayout
            android:id="@+id/linearFilterLayout"
            android:layout_width="match_parent"
            app:layout_behavior="fc.admin.fcexpressadmin.itemdecorators.FABFloatOnScroll"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin10dp"
            android:background="@color/white"
            android:orientation="horizontal"
            android:padding="@dimen/margin10dp"
            android:visibility="visible"
            android:weightSum="3">
        </LinearLayout>

        <ViewFlipper
            android:id="@+id/ViewFlipper01"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/linearFilterLayout"
            android:layout_marginBottom="@dimen/margin10dp"
            android:layout_marginTop="@dimen/margin6dp"
            android:background="@color/gray_light"
            android:visibility="visible">

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

                <Button
                    android:id="@+id/btnFooterRefresh"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="5dp"
                    android:text="Refresh"
                    android:visibility="visible"/>

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/gridview"
                    android:layout_width="match_parent"
                    android:clipToPadding="false"
                    android:layout_height="match_parent"
                    android:layout_above="@+id/btnFooterRefresh"
                    android:cacheColorHint="@android:color/transparent"
                    android:listSelector="@android:color/transparent"
                    android:scrollbars="vertical"
                    android:scrollingCache="false"
                    android:visibility="visible"/>
 </RelativeLayout>
        </ViewFlipper>
    </RelativeLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>

and this is code of my custom behaviour:

public class FABFloatOnScroll extends CoordinatorLayout.Behavior {
    private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
    private int mDySinceDirectionChange=0;

    public FABFloatOnScroll() {
        super();
    }
    public FABFloatOnScroll(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        Log.e("scroll", "dependent on views");
        return dependency instanceof LinearLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        // Adjust the child View accordingly
        Log.e("scroll","dependent");

        return true;
    }
    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
        Log.e("scroll","called");
        //child -> Floating Action Button
        if (dyConsumed > 0) {
            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
            int fab_bottomMargin = layoutParams.bottomMargin;
            child.animate().translationY(child.getHeight() + fab_bottomMargin).setInterpolator(new LinearInterpolator()).start();
        } else if (dyConsumed < 0) {
            child.animate().translationY(0).setInterpolator(new LinearInterpolator()).start();
        }
    }
    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
        if (dy > 0 && mDySinceDirectionChange < 0
                || dy < 0 && mDySinceDirectionChange > 0) {
            mDySinceDirectionChange = 0;
        }

        mDySinceDirectionChange += dy;

        if (mDySinceDirectionChange > child.getHeight()
                && child.getVisibility() == View.VISIBLE) {
            hide(child);
        } else if (mDySinceDirectionChange < 0
                && child.getVisibility() == View.GONE) {
            show(child);
        }
    }
    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
    }
    private void hide(final View view) {
        view.animate()
                .translationY(view.getHeight())
                .setInterpolator(INTERPOLATOR)
                .setDuration(200)
        .setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                // Prevent drawing the View after it is gone
                view.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animator) {
                // Canceling a hide should show the view
                show(view);
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
            }
        })
                .start();
    }
    private void show(final View view) {
        view.animate()
                .translationY(0)
                .setInterpolator(INTERPOLATOR)
                .setDuration(200)
                .setListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animator) {
                    }

                    @Override
                    public void onAnimationEnd(Animator animator) {
                        // Prevent drawing the View after it is gone
                        view.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationCancel(Animator animator) {
                        // Canceling a hide should show the view
                        hide(view);
                    }

                    @Override
                    public void onAnimationRepeat(Animator animator) {
                    }
                })
                .start();
    }
}

But the problem is the custom behaviour class is not calling no logs are printed whatsoever

like image 587
JAAD Avatar asked Jun 23 '16 11:06

JAAD


People also ask

What is the coordinator layout?

CoordinatorLayout is a super-powered FrameLayout . CoordinatorLayout is intended for two primary use cases: As a top-level application decor or chrome layout. As a container for a specific interaction with one or more child views.

How do you align items in coordinator layout?

You can try gravity to align inside the CoordinatorLayout. android:layout_gravity="end" This worked for me. Save this answer. Show activity on this post.

Is coordinator layout deprecated?

The CoordinatorLayout. DefaultBehavior annotation is deprecated.

What is CoordinatorLayout and ConstraintLayout?

Use Coordinatorlayout as the top-level application decor. It will usually used to layout AppBarLayout , FloatingActionButton , and the main body of your screen, say NestedScrollView . Inside the NestedScrollView use ConstraintLayout to describe the rest of the layout as a flat hierarchy.


1 Answers

A Behavior can be effective only when associated to a direct child of a CoordinatorLayout.

like image 167
GPack Avatar answered Sep 26 '22 14:09

GPack