10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FloatingActionButton.Behavior をカスタムして使ってみる

Last updated at Posted at 2015-06-23

device-2015-06-23-201619.gif

CustomView で定義したフッターのバーに FloatingActionButton が乗っかるのがうざいのでFloatingActionButton.Behaviorをカスタマイズしてみた。

とりあえず、ソース。

public class AwesomeView extends ViewGroup {
    public static class FabBehavior extends FloatingActionButton.Behavior {

        private AwesomeView mAwesomeView;

        public FabBehavior() {
        }

        public FabBehavior(Context context, AttributeSet attrs) {
        }

        @Override
        public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child,
                View dependency) {
            mAwesomeView = findAwesomeView(dependency);
            return mAwesomeView != null || super.layoutDependsOn(parent, child, dependency);
        }

        @Override
        public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child,
                View dependency) {
            if (mAwesomeView != null) {
                updateFabTranslationForAwesomeView(parent, child, mAwesomeView);
            }

            return super.onDependentViewChanged(parent, child, dependency);
        }

        private void updateFabTranslationForAwesomeView(CoordinatorLayout parent,
                FloatingActionButton fab, View awesomeView) {
            float translationY = this.getFabTranslationYForAwesomeView(parent, fab);
            if (translationY != this.mTranslationY) {
                ViewCompat.animate(fab).cancel();
                if (Math.abs(translationY - this.mTranslationY) == (float) awesomeView.getHeight()) {
                    ViewCompat.animate(fab)
                            .translationY(translationY)
                            .setInterpolator(INTERPOLATOR)
                            .setListener(null);
                } else {
                    ViewCompat.setTranslationY(fab, translationY);
                }

                this.mTranslationY = translationY;
            }

        }

        private float getFabTranslationYForAwesomeView(CoordinatorLayout parent,
                FloatingActionButton fab) {
            final float offset = (mAwesomeView == null) ? .0f
                    : ViewCompat.getTranslationY(mAwesomeView) - (float) mAwesomeView.getHeight();
            return Math.min(.0f, offset);
        }

        // TODO: 複数の AwesomeView 対応できるように
        private AwesomeView findAwesomeView(View view) {
            if (view instanceof AwesomeView) {
                return (AwesomeView) view;
            } else if (view instanceof ViewGroup) {
                final ViewGroup group = (ViewGroup) view;
                for (int i = 0; i < group.getChildCount(); ++i) {
                    final AwesomeView awesomeView = findAwesomeView(group.getChildAt(i));
                    if (awesomeView != null) {
                        return awesomeView;
                    }
                }
            }

            return null;
        }
    }
}

本来なら SnackBarLayout のようにレイアウトを定義し、その内の要素でレイアウトを行なうのだろうけど面倒くさいので。

10
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?