旧答案 - 见下文
。
.
好的,我正在使用处理程序执行此操作,我知道使用它们并不总是一个很好的解决方案,但这里它的任务很轻,持续 1 秒。
但是效果如我所愿这是最重要的!
如果你们有更好的解决方案,我仍然想听听你们的意见:
首先我在 onCreate() 中使用 getViewTreeObserver() 来获取我的底部表格布局的 peekheight。
bottomSheetPeekLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
bottomSheetPeekLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
peekHeight = bottomSheetPeekLayout.getHeight();
}
});
然后,在网络结果中,我只使用了 setPeekHeight,因为有一个 set animation to true 选项可用:
final Handler h1 = new Handler();
h1.postDelayed(new Runnable() {
@Override
public void run() {
h1.removeCallbacksAndMessages(this);
if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED){
bottomSheetBehavior.setPeekHeight(peekHeight*3, true);
final Handler h2 = new Handler();
h2.postDelayed(new Runnable() {
@Override
public void run() {
h2.removeCallbacksAndMessages(this);
bottomSheetBehavior.setPeekHeight(peekHeight, true);
}
}, 400);
}
}
}, 600);
。
.
更新
。
.
虽然使用自己的持续时间是一个非常糟糕的主意,但我决定使用 animate() 方法。与上述处理程序答案相比,它更流畅、更美观。
如果您想使用它,请确保您的底页足够高,以便在动画期间不会在底部创建空白区域进行翻译。
使用这个:
bottomSheet.animate()
.translationYBy(-250)
.setDuration(300)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
bottomSheet.animate()
.translationY(0)
.setDuration(300);
}
});
您可以自定义延迟,这样会更简洁 :) 享受吧!