当我向下滚动列表视图时,FAM不会消失(隐藏)

时间:2016-08-21 15:02:52

标签: android floating-action-button

当我向下滚动列表视图时,FAM不会消失(隐藏), 我使用了app:layout_behavior =“com.teroject.teroject.FloatingActionMenuBehavior” 和FloatingActionMenuBehavior java类是在代码下面,但正如我所说,什么都没发生! 我该怎么办?

主要活动的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类:

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

}

1 个答案:

答案 0 :(得分:1)

滚动行为只有在您调用listView.setNestedScrollingEnabled(true)时才能使用ListView,并且只能在api-21及更高版本上使用。

修复它的正确方法是将ListView转换为RecyclerView,它支持所有支持的api级别的嵌套滚动。