今天锨络,在重寫 FloatingActionButton.Behavior 的時候隅俘,遇到了問題攻谁,記錄一下
第一個問題,在完成了 behavior 代碼之后
public class ScrollFABBehavior extends FloatingActionButton.Behavior {
@Override
public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
@NonNull FloatingActionButton child,
@NonNull View directTargetChild,
@NonNull View target, int axes, int type) {
// Ensure we react to vertical scrolling
return axes == ViewCompat.SCROLL_AXIS_VERTICAL
|| super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes);
}
@Override
public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
@NonNull FloatingActionButton child,
@NonNull View target,
int dxConsumed,
int dyConsumed,
int dxUnconsumed,
int dyUnconsumed,
int type) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
child.hide();
} else if (dyConsumed <= 0 && child.getVisibility() != View.VISIBLE) {
child.show();
}
}
}
用在 FAB 上的時候 出現(xiàn)了 異常
Could not inflate Behavior subclass com.example.....ScrollFABBehavior
仔細(xì)一看断盛,shit,忘記了 從 xml 加載對應(yīng)的 構(gòu)造函數(shù)
加上就解決了
public ScrollFABBehavior(Context context, AttributeSet attrs) {
super();
}
接下來是第二個問題
FAB 的 hide() 沒有問題愉舔,可以hide 完之后 不show 了钢猛。
跟蹤一下發(fā)現(xiàn),onNestedScroll() 只調(diào)用了一次
/**
* Called when a nested scroll in progress has updated and the target has scrolled or
* attempted to scroll.
*
* <p>Any Behavior associated with the direct child of the CoordinatorLayout may elect
* to accept the nested scroll as part of {@link #onStartNestedScroll}. Each Behavior
* that returned true will receive subsequent nested scroll events for that nested scroll.
* </p>
*
* <p><code>onNestedScroll</code> is called each time the nested scroll is updated by the
* nested scrolling child, with both consumed and unconsumed components of the scroll
* supplied in pixels. <em>Each Behavior responding to the nested scroll will receive the
* same values.</em>
* </p>
*
* @param coordinatorLayout the CoordinatorLayout parent of the view this Behavior is
* associated with
* @param child the child view of the CoordinatorLayout this Behavior is associated with
* @param target the descendant view of the CoordinatorLayout performing the nested scroll
* @param dxConsumed horizontal pixels consumed by the target's own scrolling operation
* @param dyConsumed vertical pixels consumed by the target's own scrolling operation
* @param dxUnconsumed horizontal pixels not consumed by the target's own scrolling
* operation, but requested by the user
* @param dyUnconsumed vertical pixels not consumed by the target's own scrolling operation,
* but requested by the user
* @param type the type of input which cause this scroll event
*
* @see NestedScrollingParent2#onNestedScroll(View, int, int, int, int, int)
*/
這好像是在逗我屑宠?我也看不懂厢洞,大概意思就是 這個函數(shù)在 scroll 的時候就會更新
可是并沒有
解決辦法:
@Override
public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
@NonNull FloatingActionButton child,
@NonNull View target,
int dxConsumed,
int dyConsumed,
int dxUnconsumed,
int dyUnconsumed,
int type) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
child.hide(new FloatingActionButton.OnVisibilityChangedListener() {
@Override
public void onShown(FloatingActionButton fab) {
super.onShown(fab);
}
@Override
public void onHidden(FloatingActionButton fab) {
super.onHidden(fab);
fab.setVisibility(View.INVISIBLE);
}
});
} else if (dyConsumed <= 0 && child.getVisibility() != View.VISIBLE) {
child.show();
}
}
跟上面的區(qū)別就是 hide 的時候 別把 FAB visibility 變成GONE,而是變成 INVISIBLE
GONE 與 INVISIBLE 的區(qū)別就不說了典奉。
為什么會發(fā)生這種情況呢躺翻?
道聽途說:FAB都GONE 掉了,就不跟蹤Scroll狀態(tài)了卫玖,所以也就沒有 onNestedScroll() 什么事公你,但是INVISIBLE 就不同了,會很勤奮地做這件事情假瞬,雖然看不見陕靠。
已發(fā)布至 我說的這句話是謊話