Ali Soleymani Roozbahani Ali Soleymani Roozbahani - 3 months ago 12
Android Question

The FAM doesn't disappear(hide) when i scroll down the listview

the FAM doesn't disappear(hide) when i scroll down the listview,
i used app:layout_behavior="com.teroject.teroject.FloatingActionMenuBehavior"
and the FloatingActionMenuBehavior java class is below code , but as i said nothing happend!
what should i do?

the main activity's xml :

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/tools">
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</include>

<!-- This LinearLayout represents the contents of the screen -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/flContent"
android:orientation="vertical">

<!--TabLayout XML-->
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_tab"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
android:background="@color/colorPrimary"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<com.github.clans.fab.FloatingActionMenu
android:id="@+id/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:menu_fab_size="normal"
app:menu_showShadow="true"
app:menu_shadowColor="#66000000"
app:menu_shadowRadius="1dp"
app:menu_shadowXOffset="1dp"
app:menu_shadowYOffset="1dp"
app:menu_colorNormal="#2e782e"
app:menu_colorPressed="#2aa749"
app:menu_colorRipple="#99FFFFFF"
app:menu_animationDelayPerItem="80"
app:menu_icon="@drawable/fab_add"
app:menu_buttonSpacing="0dp"
app:menu_labels_margin="0dp"
app:menu_labels_showAnimation="@anim/fab_slide_in_from_right"
app:menu_labels_hideAnimation="@anim/fab_slide_out_to_right"
app:menu_labels_paddingTop="4dp"
app:menu_labels_paddingRight="8dp"
app:menu_labels_paddingBottom="4dp"
app:menu_labels_paddingLeft="8dp"
app:menu_labels_padding="8dp"
app:menu_labels_textColor="#FFFFFF"
app:menu_labels_textSize="14sp"
app:menu_labels_cornerRadius="3dp"
app:menu_labels_colorNormal="#08ae7c"
app:menu_labels_colorPressed="#71e1b0"
app:menu_labels_colorRipple="#99FFFFFF"
app:menu_labels_showShadow="true"
app:menu_labels_singleLine="false"
app:menu_labels_ellipsize="none"
app:menu_labels_maxLines="-1"
app:menu_labels_position="left"
app:menu_openDirection="up"
app:menu_backgroundColor="@android:color/transparent"
android:layout_margin="@dimen/fab_margin"
android:layout_gravity="bottom|right"
app:layout_behavior="com.teroject.teroject.FloatingActionMenuBehavior">


<com.github.clans.fab.FloatingActionButton
android:id="@+id/menu_item_creating_team"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_add_team"
app:fab_size="mini"
app:fab_colorNormal="#08ae7c"
app:fab_colorPressed="#71e1b0"
app:fab_colorRipple="#99FFFFFF"
app:fab_label="ایجاد تیم"/>

<com.github.clans.fab.FloatingActionButton
android:id="@+id/menu_item_creating_project"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_add_project"
app:fab_size="mini"
app:fab_colorNormal="#08ae7c"
app:fab_colorPressed="#71e1b0"
app:fab_colorRipple="#99FFFFFF"
app:fab_label="ایجاد پروژه"/>
<com.github.clans.fab.FloatingActionButton
android:id="@+id/menu_item_creating_duty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_add_duty"
app:fab_size="mini"
app:fab_colorNormal="#08ae7c"
app:fab_colorPressed="#71e1b0"
app:fab_colorRipple="#99FFFFFF"
app:fab_label="ایجاد وظیفه"/>
<com.github.clans.fab.FloatingActionButton
android:id="@+id/menu_item_sending_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_new_message"
app:fab_size="mini"
app:fab_colorNormal="#08ae7c"
app:fab_colorPressed="#71e1b0"
app:fab_colorRipple="#99FFFFFF"
app:fab_label="ارسال پیام"/>

</com.github.clans.fab.FloatingActionMenu>


</android.support.design.widget.CoordinatorLayout>

</LinearLayout>
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="@color/colorNavDrawerBackColor"
app:menu="@menu/drawer_view"
app:theme="@style/nav_drawer_style" />
</android.support.v4.widget.DrawerLayout>




FloatingActionMenuBehavior.java class:

public class FloatingActionMenuBehavior extends CoordinatorLayout.Behavior {
private float mTranslationY;

public FloatingActionMenuBehavior(Context context, AttributeSet attrs) {
super();
}

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
if (child instanceof FloatingActionMenu && dependency instanceof Snackbar.SnackbarLayout) {
this.updateTranslation(parent, child, dependency);
}

return false;
}

private void updateTranslation(CoordinatorLayout parent, View child, View dependency) {
float translationY = this.getTranslationY(parent, child);
if (translationY != this.mTranslationY) {
ViewCompat.animate(child)
.cancel();
if (Math.abs(translationY - this.mTranslationY) == (float) dependency.getHeight()) {
ViewCompat.animate(child)
.translationY(translationY)
.setListener((ViewPropertyAnimatorListener) null);
} else {
ViewCompat.setTranslationY(child, translationY);
}

this.mTranslationY = translationY;
}

}

private float getTranslationY(CoordinatorLayout parent, View child) {
float minOffset = 0.0F;
List dependencies = parent.getDependencies(child);
int i = 0;

for (int z = dependencies.size(); i < z; ++i) {
View view = (View) dependencies.get(i);
if (view instanceof Snackbar.SnackbarLayout && parent.doViewsOverlap(child, view)) {
minOffset = Math.min(minOffset, ViewCompat.getTranslationY(view) - (float) view.getHeight());
}
}

return minOffset;
}

/**
* onStartNestedScroll and onNestedScroll will hide/show the FabMenu when a scroll is detected.
*/
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child,
View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL ||
super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target,
nestedScrollAxes);
}

@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);
FloatingActionMenu fabMenu = (FloatingActionMenu) child;
if (dyConsumed > 0 && !fabMenu.isMenuButtonHidden()) {
fabMenu.hideMenuButton(true);
} else if (dyConsumed < 0 && fabMenu.isMenuButtonHidden()) {
fabMenu.showMenuButton(true);
}
}


}

Answer

Scrolling behavior will only work with a ListView if you call listView.setNestedScrollingEnabled(true), and with that it will only work on api-21 and up.

The proper way to fix it is to convert your ListView to a RecyclerView, which supports nested scrolling on all supported api levels.

Comments