I am trying to create an activity with 2 FAB on its CollapsingToolbarLayout
.
I created a regular scrolling activity (which creates with 1 FAB that works great and hide when toolbar is getting collapsed).
Now, I tried to add another FAB, but i can't put it beside the first FAB (can put it only in start/center/end of AppBarLayout
.
Also, I tried to put the 2 FAB inside LinearLayout
. It looks good, but the FAB are not hiding when the toolbar is getting collapsed.
Here is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data class="MomentActivityBinder">
<variable
name="momentViewModel"
type="com.infibond.infi.timeline.mvp.moment.MomentViewModel"/>
</data>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.infibond.infi.timeline.mvp.moment.TimelineMomentActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include
android:id="@+id/moment_content"
layout="@layout/timeline_content_moment"
app:momentViewModel="@{momentViewModel}"/>
<!--two FABs without linear layout -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"
android:orientation="horizontal">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_infi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_baseX1"
android:src="@drawable/ic_radio_button_checked_black_24px"
android:tint="@color/gray"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"
app:backgroundTint="@color/white"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_baseX1"
android:src="@drawable/ic_share_mobile_black_24px"
android:tint="@color/gray"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"
app:backgroundTint="@color/white"/>
</LinearLayout>
<!--one FAB without linear layout -->
<!--<android.support.design.widget.FloatingActionButton-->
<!--android:id="@+id/fab_share"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_margin="@dimen/margin_baseX1"-->
<!--android:src="@drawable/ic_share_mobile_black_24px"-->
<!--android:tint="@color/gray"-->
<!--app:layout_anchor="@id/app_bar"-->
<!--app:layout_anchorGravity="bottom|end"-->
<!--app:backgroundTint="@color/white"/>
</android.support.design.widget.CoordinatorLayout>
</layout>
You can use AppbarlayoutChangeListener and on different states manually hide and show the FAB
mAppBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
@Override
public void onStateChanged(AppBarLayout appBarLayout, State state) {
switch (state) {
case COLLAPSED:
fab1.hide();
fab2.hide();
break;
case EXPANDED:
fab1.show();
fab2.show();
break;
case IDLE:
fab1.show();
fab2.show();
break;
}
}
});
AppBarStateChangeListener.java
public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {
public enum State {
EXPANDED,
COLLAPSED,
IDLE
}
private State mCurrentState = State.IDLE;
@Override
public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
if (i == 0) {
if (mCurrentState != State.EXPANDED) {
onStateChanged(appBarLayout, State.EXPANDED);
}
mCurrentState = State.EXPANDED;
} else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
if (mCurrentState != State.COLLAPSED) {
onStateChanged(appBarLayout, State.COLLAPSED);
}
mCurrentState = State.COLLAPSED;
} else {
if (mCurrentState != State.IDLE) {
onStateChanged(appBarLayout, State.IDLE);
}
mCurrentState = State.IDLE;
}
}
public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
}