dim.red
新出的design:23.2.0 引入了一個(gè)新的Behavior :BottomSheetBehavior
問題
設(shè)置 BottomSheetBehavior 沒有設(shè)置 PeekHeight 或者 PeekHeight為0 的時(shí)候. 底部的BottomSheetBehavior 視圖滑動(dòng)不出來(lái).
分析
根據(jù)進(jìn)入源碼,具體的問題是
@Override
public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection) {
// First let the parent lay it out
if (mState != STATE_DRAGGING && mState != STATE_SETTLING) {
parent.onLayoutChild(child, layoutDirection);
}
// Offset the bottom sheet
mParentHeight = parent.getHeight();
mMinOffset = Math.max(0, mParentHeight - child.getHeight());
mMaxOffset = mParentHeight - mPeekHeight;
if (mState == STATE_EXPANDED) {
ViewCompat.offsetTopAndBottom(child, mMinOffset);
} else if (mHideable && mState == STATE_HIDDEN) {
ViewCompat.offsetTopAndBottom(child, mParentHeight);
} else if (mState == STATE_COLLAPSED) {
ViewCompat.offsetTopAndBottom(child, mMaxOffset);
}
if (mViewDragHelper == null) {
mViewDragHelper = ViewDragHelper.create(parent, mDragCallback);
}
mViewRef = new WeakReference<>(child);
mNestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));
return true;
}
一開始view 的位置是屏幕中的.因?yàn)?mState 的初始狀態(tài)為STATE_COLLAPSED,mPeekHeight 為0,
mMaxOffset 變成了 rv 的高度,所以view 馬上就被移出了屏幕.
然后后面View就全程透明了.很奇葩.
解決方案
解決方案 1
主動(dòng)發(fā)起requestLayout 強(qiáng)制重繪.
behavior.setBottomSheetCallback(new BottomSheet.BottomSheetCallback() {
public boolean hasRequest;
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
if (!hasRequest && behavior.getPeekHeight() == 0 && slideOffset > 0) {
hasRequest = true;
bottomSheet.requestLayout();
}
}
});
缺點(diǎn)是使用了requestLayout 方法.
解決方案2
這是幸運(yùn)Science提供的
bottomSheet.setTranslationY(statusbarheight);
behavior.setBottomSheetCallback(new BottomSheet.BottomSheetCallback() {
public boolean hasRequest;
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
if (!hasRequest && behavior.getPeekHeight() == 0 && slideOffset > 0) {
hasRequest = true;
bottomSheet.setTranslationY(0);
}
}
});
通過設(shè)置TranslationY
的差值.讓他重繪出來(lái).
恍惚間記得google Design 包下有類似的解決方案,同樣使用的是TranslationY
差值的方式.
類:android.support.design.widget.ViewOffsetHelper
方法:updateOffsets
private void updateOffsets() {
ViewCompat.offsetTopAndBottom(mView, mOffsetTop - (mView.getTop() - mLayoutTop));
ViewCompat.offsetLeftAndRight(mView, mOffsetLeft - (mView.getLeft() - mLayoutLeft));
// Manually invalidate the view and parent to make sure we get drawn pre-M
if (Build.VERSION.SDK_INT < 23) {
tickleInvalidationFlag(mView);
final ViewParent vp = mView.getParent();
if (vp instanceof View) {
tickleInvalidationFlag((View) vp);
}
}
}
private static void tickleInvalidationFlag(View view) {
final float y = ViewCompat.getTranslationY(view);
ViewCompat.setTranslationY(view, y + 1);
ViewCompat.setTranslationY(view, y);
}
可以看出這是一個(gè)23以下都有可能存在的bug.
同時(shí)需要注意的是ViewOffsetHelper
是一個(gè)私有的類,updateOffsets
是一個(gè)私有的方法.所以你只能copy,不能直接使用.
最終的方案
就是在方案2的基礎(chǔ)上做一點(diǎn)小修改,改使用google的方案.
behavior.setBottomSheetCallback(new BottomSheet.BottomSheetCallback() {
public boolean hasRequest;
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
if (!hasRequest && behavior.getPeekHeight() == 0 && slideOffset > 0) {
hasRequest = true;
updateOffsets(bottomSheet);
}
}
});
private void updateOffsets(View view) {
// Manually invalidate the view and parent to make sure we get drawn pre-M
if (Build.VERSION.SDK_INT < 23) {
tickleInvalidationFlag(view)
final ViewParent vp = view.getParent();
if (vp instanceof View) {
tickleInvalidationFlag((View) vp);
}
}
}
private static void tickleInvalidationFlag(View view) {
final float y = ViewCompat.getTranslationY(view);
ViewCompat.setTranslationY(view, y + 1);
ViewCompat.setTranslationY(view, y);
}
尾巴
google 在開發(fā)這個(gè)的時(shí)候應(yīng)該并沒有考慮這樣的應(yīng)用場(chǎng)景. 如果場(chǎng)景是這樣的話可以是使用BottomSheetDialog
這個(gè)代替.