Android support 23.2 使用BottomShe

作者: 轻微 | 来源:发表于2016-02-26 01:14 被阅读8498次

dim.red
新出的design:23.2.0 引入了一个新的Behavior :BottomSheetBehavior

问题

设置 BottomSheetBehavior 没有设置 PeekHeight 或者 PeekHeight为0 的时候. 底部的BottomSheetBehavior 视图滑动不出来.

分析

根据进入源码,具体的问题是



@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 的位置是屏幕中的.因为 mState 的初始状态为STATE_COLLAPSED,mPeekHeight 为0,
mMaxOffset 变成了 rv 的高度,所以view 马上就被移出了屏幕.
然后后面View就全程透明了.很奇葩.

解决方案

解决方案 1

主动发起requestLayout 强制重绘.


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();
        }
    }
});

缺点是使用了requestLayout 方法.

解决方案2

这是幸运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);
        }
    }
});

通过设置TranslationY的差值.让他重绘出来.
恍惚间记得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);
}

可以看出这是一个23以下都有可能存在的bug.
同时需要注意的是ViewOffsetHelper是一个私有的类,updateOffsets是一个私有的方法.所以你只能copy,不能直接使用.

最终的方案

就是在方案2的基础上做一点小修改,改使用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 在开发这个的时候应该并没有考虑这样的应用场景. 如果场景是这样的话可以是使用BottomSheetDialog 这个代替.

相关文章

网友评论

  • b4a646582784:24.2.0 好像没有这个bug,直接设置app:behavior_peekHeight="0dp" 好像是可以的
  • Alien的小窝:嗯,谢谢,简书有搜索功能吗,发现写文章多了,找着太麻烦了
  • 1d1d0de3591f:BottomSheetBehavior太多坑了,四大坑解决了两个,我在里面viewpager嵌套viewpager,结果切换fragment会发现内容没有刷新,如果所有界面都刷新过,则可以正常切换,就算在界面上一个控件的隐藏和现实,也会出现同样的问题
  • Alien的小窝:BottomSheetDialog如何使用,
    BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
    bottomSheetDialog.setContentView(R.layout.layout_bottom_sheet);
    bottomSheetDialog.show();
    默认不显示,一拉可以出来,然后就dismiss了
    Jinwong:@编程之乐 http://www.jianshu.com/p/38af0cf77352 这篇我写,主要讲BottomSheetDialog,可以参考一下
  • 1d1d0de3591f:我是一出来设置setPeekHeight,然后下拉隐藏,然后通过点击显示,有时候透明有时候又显示出来,不知道是不是和楼主同一个问题
    Jinwong:@lhwalq 你是用Dialog吗?Dialog 如果仅仅是show的话,会存在有时为透明的情况,因为此时高度为0,可以每次调用的时候,直接new 出来
    轻微:@lhwalq public void onSlide(@NonNull View bottomSheet, float slideOffset) {
    updateOffsets(bottomSheet);
    } 这样试试看
  • 1d1d0de3591f:po主,我也出现这个问题,用了你的解决方案,为什么还是依然会出现?没解决问题啊,能看到你的源码吗
    1d1d0de3591f:@轻微 为什么用了你的最终解决方案,点击还是会透明?
    轻微:@lhwalq 源码都在上面了
  • 7768b209fbfa:的确存在这个问题,谢谢Po主
  • 48674505dc48:并不存在你说的【behavior_peekHeight】不设置或者设置为0就不出来的问题啊。
    轻微:@有志男青年 在5.0上面
  • SScience:在5.1和6.0上测试,并没有你所说的bug啊??
    06peng:不能上拉吧,一触摸就dismiss了。不过后来我给contentView加了固定的高度就可以了。
    SScience:@轻微 不设置mPeekHeight或者mPeekHeight=0,5.0,5.1,6.0都可以上拉出来~~亲测
    轻微:@幸运Science mPeekHeight 为0 ,上拉能出来?
  • 陆地蛟龙:6的飞起。
  • 泥阿布_Abner:速度好快

本文标题:Android support 23.2 使用BottomShe

本文链接:https://www.haomeiwen.com/subject/lwxwkttx.html