【问题标题】:How to enable/disable FloatingActionButton Behavior如何启用/禁用 FloatingActionButton 行为
【发布时间】:2016-05-05 06:35:18
【问题描述】:

我正在开发某个片段中的应用程序,我想隐藏 FloatingActionButton。 当我设置 android:visibility="gone".当我上下滑动时,行为动画会显示 FloatingActionButton。有什么方法可以禁用/启用 FloatingActionButton 行为。

谢谢你。

这是我的代码

QuickReturnFooterBehavior.java

package com.app.common;

import android.animation.Animator;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewPropertyAnimator;


@SuppressWarnings("unused")
public class QuickReturnFooterBehavior extends     CoordinatorLayout.Behavior<View> {
private static final FastOutSlowInInterpolator INTERPOLATOR = new FastOutSlowInInterpolator();

private int mDySinceDirectionChange;
private boolean mIsShowing;
private boolean mIsHiding;

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

@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
    return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
}

@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
    if (dy > 0 && mDySinceDirectionChange < 0
            || dy < 0 && mDySinceDirectionChange > 0) {
        // We detected a direction change- cancel existing animations and reset our cumulative delta Y
        child.animate().cancel();
        mDySinceDirectionChange = 0;
    }

    mDySinceDirectionChange += dy;

    if (mDySinceDirectionChange > child.getHeight()
            && child.getVisibility() == View.VISIBLE
            && !mIsHiding) {
        hide(child);
    } else if (mDySinceDirectionChange < 0
            && child.getVisibility() == View.GONE
            && !mIsShowing) {
        show(child);
    }
}

/**
 * Hide the quick return view.
 *
 * Animates hiding the view, with the view sliding down and out of the screen.
 * After the view has disappeared, its visibility will change to GONE.
 *
 * @param view The quick return view
 */
private void hide(final View view) {
    mIsHiding = true;
    ViewPropertyAnimator animator = view.animate()
            .translationY(view.getHeight())
            .setInterpolator(INTERPOLATOR)
            .setDuration(200);

    animator.setListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animator) {}

        @Override
        public void onAnimationEnd(Animator animator) {
            // Prevent drawing the View after it is gone
            mIsHiding = false;
            view.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationCancel(Animator animator) {
            // Canceling a hide should show the view
            mIsHiding = false;
            if (!mIsShowing) {
                show(view);
            }
        }

        @Override
        public void onAnimationRepeat(Animator animator) {}
    });

    animator.start();
}

/**
 * Show the quick return view.
 *
 * Animates showing the view, with the view sliding up from the bottom of the screen.
 * After the view has reappeared, its visibility will change to VISIBLE.
 *
 * @param view The quick return view
 */
private void show(final View view) {
    mIsShowing = true;
    ViewPropertyAnimator animator = view.animate()
            .translationY(0)
            .setInterpolator(INTERPOLATOR)
            .setDuration(200);

    animator.setListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animator) {
            view.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animator animator) {
            mIsShowing = false;
        }

        @Override
        public void onAnimationCancel(Animator animator) {
            // Canceling a show should hide the view
            mIsShowing = false;
            if (!mIsHiding) {
                hide(view);
            }
        }

        @Override
        public void onAnimationRepeat(Animator animator) {}
    });

    animator.start();
  }
 }

和 XML

   <android.support.design.widget.FloatingActionButton
    app:layout_behavior="com.app.common.QuickReturnFooterBehavior"
    android:id="@+id/fab_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:src="@drawable/ic_action_quick_response_code"
    app:backgroundTint="@color/text_gray"
    app:descriptionText="@string/add_friend"
    app:elevation="3dp"
    app:borderWidth="0dp"
    />

【问题讨论】:

    标签: android android-coordinatorlayout floating-action-button


    【解决方案1】:

    终于找到了解决办法,想和大家分享一下。

    您可以启用/禁用 FloatingActionButton 行为

    禁用行为

        FloatingActionButton fab2 = (FloatingActionButton)findViewById(R.id.fab2);
    
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) fab2.getLayoutParams();
        params.setBehavior(null);
        fab2.requestLayout();
        fab2.setVisibility(View.GONE);
    

    启用行为

        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) fab2.getLayoutParams();
        params.setBehavior(new QuickReturnFooterBehavior());
        fab2.requestLayout();
        fab2.setVisibility(View.VISIBLE);
    

    已编辑:更多可重用类

    public class CoordinateBehaviourUtils {
    
      public static void enableDisableViewBehaviour(View view,CoordinatorLayout.Behavior<View> behavior,boolean enable){
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) view.getLayoutParams();
        params.setBehavior(behavior);
        view.requestLayout();
        view.setVisibility((enable ? View.VISIBLE: View.GONE));
      }
    
    }
    

    如何启用公共类

    FloatingActionButton fab2 = (FloatingActionButton)findViewById(R.id.fab2);
    CoordinateBehaviourUtils.enableDisableViewBehaviour(fab2,new QuickReturnFooterBehavior(),true);
    

    如何使用通用类禁用

    FloatingActionButton fab2 = (FloatingActionButton)findViewById(R.id.fab2);
    CoordinateBehaviourUtils.enableDisableViewBehaviour(fab2,null,false);
    

    希望它能解决你的问题:)

    【讨论】:

      【解决方案2】:

      在设置 FAB 的可见性时没有区别,它可能适用于其他控件。

      0 is for VISIBLE
      4 is for INVISIBLE
      8 is for GONE
      

      你可以试试这个;

      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      // to make is disable for some requirement 
      fab.setVisibility(View.GONE);
      // to make it enable 
      fab.setVisibility(View.VISIBLE);
      

      【讨论】:

        猜你喜欢
        • 2012-09-24
        • 2020-12-24
        • 2012-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-28
        • 1970-01-01
        相关资源
        最近更新 更多