最近在使用FloatingActionButton的时候,自定义了其Behavior,然后发现在SDK在25及以上的时候,出现了只能隐藏不能重新出现的问题(24及以下没有出现此问题),Behavior代码如下:
public class MyFabBehavior extends FloatingActionButton.Behavior {
// 必须重写两个参数的构造方法,否则会报错
public MyFabBehavior (Context context, AttributeSet attrs) {
super(context, attrs);
}
// 返回true时表示传递滑动参数,同时执行后面的滑动监听,返回false的话后面的onNestedScroll等方法就不会调用了
// super是直接返回的false
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
// 判断如果是垂直滚动则返回true
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
// 可以直接删掉super 进去之后发现super里面啥也没做
// super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
System.out.println("dyConsumed: " + dyConsumed);
// 如果向上滑动 则隐藏
if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
child.hide();
} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
// 向下则显示
child.show();
}
}
}
后来进入CoordinatorLayout里面看了下,在该类的onNestedScroll()方法中对比24版本和25版本的SDK,发现25多了一点代码
@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
int dxUnconsumed, int dyUnconsumed) {
final int childCount = getChildCount();
boolean accepted = false;
for (int i = 0; i < childCount; i++) {
final View view = getChildAt(i);
if (view.getVisibility() == GONE) { // 这个判断就是比24多出的
// If the child is GONE, skip...
continue;
}
final LayoutParams lp = (LayoutParams) view.getLayoutParams();
if (!lp.isNestedScrollAccepted()) {
continue;
}
final Behavior viewBehavior = lp.getBehavior();
if (viewBehavior != null) {
viewBehavior.onNestedScroll(this, view, target, dxConsumed, dyConsumed,
dxUnconsumed, dyUnconsumed); // Behavior对事件的响应
accepted = true;
}
}
if (accepted) {
onChildViewsChanged(EVENT_NESTED_SCROLL);
}
}
就是因为多了上面那一行判断,而我们在自定义中Behavior中hide()的时候,将FloatingActionButton隐藏了,导致代码执行到上述标红部分的时候,就直接跳出了for循环,那就没法回调onNestedScroll()方法了。
所以解决方案就是:
不要用hide()方法,也不要使用setVisibility(GONE),可以使用setVisibility(INVISIBLE),或者将其移除屏幕显示范围来达到隐藏的效果
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)