FloatingActionButton.Behavior

2019-11-21  本文已影响0人  细雨么么
package com.vortex.test;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator (chenPS) on 2019/11/21.
 */
public class MyHehavior extends FloatingActionButton.Behavior {

  /** 构造方法必须得有
*/
    public MyHehavior() {
    }

    public MyHehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
  
/**
*竖直方向必须得有
*/
    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull FloatingActionButton child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {

        return axes==CoordinatorLayout.SCROLL_AXIS_VERTICAL;
    }

    @Override
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
                               @NonNull final FloatingActionButton child,
                               @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
    //隐藏原来的hide是有问题的 不会再显示出来 调用setinvisible  而且可以在此处添加 show hide的监听
        if((dyConsumed>0||dyConsumed>0)&&child.getVisibility()==View.VISIBLE){
           child.hide(new FloatingActionButton.OnVisibilityChangedListener() {
               @Override
               public void onHidden(FloatingActionButton fab) {
                   super.onHidden(fab);
                   fab.setVisibility(View.INVISIBLE);
               }
           });
        }else if((dyConsumed<0||dyConsumed<0)&&child.getVisibility()!=View.VISIBLE){
            child.show(new FloatingActionButton.OnVisibilityChangedListener() {
                @Override
                public void onShown(FloatingActionButton fab) {
                    super.onShown(fab);
                    fab.setVisibility(View.VISIBLE);
                }
            });
        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读