当前位置: 移动技术网 > 移动技术>移动开发>Android > Android自定义FloatingActionButton滑动行为只隐藏不出现的问题小结

Android自定义FloatingActionButton滑动行为只隐藏不出现的问题小结

2019年07月24日  | 移动技术网移动技术  | 我要评论

先来段behavior代码,网上关于floatingactionbutton(以下简称fab)滑动的代码很多了,参考一下。

public class fabbehavior extends floatingactionbutton.behavior{
  private static final interpolator interpolator = new fastoutslowininterpolator();
  private boolean misanimatingout = false;
  public fabbehavior(context context, attributeset attrs) {
    super();
  }
  @override
  public boolean onstartnestedscroll(final coordinatorlayout coordinatorlayout, final floatingactionbutton child,
                    final view directtargetchild, final view target, final int nestedscrollaxes) {
    // ensure we react to vertical scrolling
    return nestedscrollaxes == viewcompat.scroll_axis_vertical
        || super.onstartnestedscroll(coordinatorlayout, child, directtargetchild, target, nestedscrollaxes);
  }
  @override
  public void onnestedscroll(final coordinatorlayout coordinatorlayout, final floatingactionbutton child,
                final view target, final int dxconsumed, final int dyconsumed,
                final int dxunconsumed, final int dyunconsumed) {
    super.onnestedscroll(coordinatorlayout, child, target, dxconsumed, dyconsumed, dxunconsumed, dyunconsumed);
    if (dyconsumed > 0 && !this.misanimatingout && child.getvisibility() == view.visible) {
      // user scrolled down and the fab is currently visible -> hide the fab
      animateout(child);
    } else if (dyconsumed < 0 && child.getvisibility() != view.visible) {
      // user scrolled up and the fab is currently not visible -> show the fab
      animatein(child);
    }
  }
  // same animation that floatingactionbutton.behavior uses to hide the fab when the appbarlayout exits
  private void animateout(final floatingactionbutton button) {
    if (build.version.sdk_int >= 14) {
      viewcompat.animate(button).translationy(button.getheight() + getmarginbottom(button)).setinterpolator(interpolator).withlayer()
          .setlistener(new viewpropertyanimatorlistener() {
            public void onanimationstart(view view) {
              fabbehavior.this.misanimatingout = true;
            }
            public void onanimationcancel(view view) {
              fabbehavior.this.misanimatingout = false;
            }
            public void onanimationend(view view) {
              fabbehavior.this.misanimatingout = false;
              view.setvisibility(view.gone);
            }
          }).start();
    } else {
    }
  }
  // same animation that floatingactionbutton.behavior uses to show the fab when the appbarlayout enters
  private void animatein(floatingactionbutton button) {
    button.setvisibility(view.visible);
    if (build.version.sdk_int >= 14) {
      viewcompat.animate(button).translationy(0)
          .setinterpolator(interpolator).withlayer().setlistener(null)
          .start();
    } else {
    }
  }
  private int getmarginbottom(view v) {
    int marginbottom = 0;
    final viewgroup.layoutparams layoutparams = v.getlayoutparams();
    if (layoutparams instanceof viewgroup.marginlayoutparams) {
      marginbottom = ((viewgroup.marginlayoutparams) layoutparams).bottommargin;
    }
    return marginbottom;
  }
}

这是自定义的一个behavior类,主要在onnestedscroll中自定义了出现和消失的动画。使用的时候,在xml文件中给fab加一个包含完整behavior类名的layout_behavior属性

app:layout_behavior="com.normalframe.widgets.view.fabbehavior"

这样fab就会随着列表上滑消失,下滑出现。这个功能主要是要处理fab的位置会使列表最后一项被挡住的问题,当上滑时,fab隐藏,这样当到达列表底部最后一项时,由于刚刚的动作是上滑动作,所以fab处于隐藏状态,不会遮挡到列表。

在写这个功能时,发现了一个问题:

上滑时fab能够正常隐藏,但是下拉列表时,fab就不出现了。

而一样的代码如果放到其它项目中,有些又可以正常实现功能。debug的时候发现,上拉时会调用onnestedscroll,于是其中自定义的隐藏方法可以被调用,但下滑时,不调用onnestedscroll。

以上所述是小编给大家介绍的android自定义floatingactionbutton滑动行为只隐藏不出现的问题小结,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网