A
A
Alexander Dorofeev2015-11-21 17:13:32
Android
Alexander Dorofeev, 2015-11-21 17:13:32

How to move FloatingActionButton down?

I have a ScrollingActivity.FAB attached to an AppBarLayout.

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_check_white_48dp"
        app:layout_anchor="@id/carSelectAppBar"
        app:layout_anchorGravity="bottom|end"
        />

I want that when the CollapsingToolBarLayout is destroyed, the button is attached to the include element in the ScrollingActivity.
That is, it was detached from the AppBar and flew down.
I am using the following code:
mToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.carSelectToolbar);
        mAppBarLayout = (AppBarLayout) findViewById(R.id.carSelectAppBar);
        mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            int scrollRange = -1;
            boolean isShow = false;

            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                if (scrollRange == -1) {
                    scrollRange = appBarLayout.getTotalScrollRange();
                }
                if (scrollRange + verticalOffset == 0) {
                    isShow = true;
                    changeFloatButtonState(isShow,fab);
                } else if (isShow) {
                    isShow = false;
                    changeFloatButtonState(isShow,fab);
                }

            }
        });

private void changeFloatButtonState(boolean isShow,FloatingActionButton fab) {
        if (!isShow) {
            CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
            params.setAnchorId(R.id.carSelectAppBar);
            params.anchorGravity = Gravity.BOTTOM | Gravity.END;
            fab.setLayoutParams(params);

        } else {
            CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
            params.setAnchorId(R.id.carSelectContentLayout);
            params.anchorGravity = Gravity.BOTTOM | Gravity.END;
            fab.setLayoutParams(params);
        }

    }

But the code throws an Exception:
An anchor may not be changed after CoordinatorLayout measurement begins before layout is complete.

What should I do? I tried to process this event in other ways, but it did not work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasilij Aleksashin, 2015-11-26
@TerraMorf

Where is it called?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question