具有协调器布局的自定义行为

时间:2016-06-23 11:44:20

标签: android android-coordinatorlayout androiddesignsupport

我试图隐藏并在我的recyclerview借助协调员布局滚动时显示视图。

我的观点是带有按钮的linearlayout,它不是fabtoolbartablayout,因为我已经知道如何在滚动时隐藏它们。

请注意,这不是重复,因为所有答案都显示如何制作工具栏或tablayout

这是我正在使用的xml

<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>

这是我自定义行为的代码:

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();
    }
}

但问题是自定义行为类没有调用没有打印任何日志

1 个答案:

答案 0 :(得分:1)

行为只有在与CoordinatorLayout的直接子项关联时才有效。