Custom Behavior with Coordinator Layout

I am trying to hide and show the view when my recyclerview scrolls using the layout coordinator.

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

Please note that this is not a duplicate, as all answers show how to use the toolbar or tab

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 the code of my user behavior:

 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 that the custom behavior class does not trigger any logs that print at all

+7
android android-coordinatorlayout androiddesignsupport
source share
1 answer

A Behavior can only be effective when associated with a direct descendant of the Layout coordinator.

+1
source share

All Articles