BIG UPDATE 避免重复代码,我提供了一个链接到完整答案的地方,你可以在那里找到有关如何获得像 Google 地图一样的完整行为的所有解释。
我想调整它的最大展开高度。我该怎么做?
BottomSheet
和BottomSheetDialogFragment
都使用Support Library 23.x中可找到的BottomSheetBehavior。该 Java 类具有两种不同用途的 mMinOffset
,其中一个用于定义它将用于绘制内容的父级区域(可能是 NestedScrollView
)。另一种用法是定义扩展锚点,也就是说,如果您将其向上滑动以形成STATE_COLLAPSED
,它将动画显示您的 BottomSheet
直到达到此锚点,但如果您仍然保持向上滑动以覆盖父级高度(CoordiantorLayout Height),则会继续展开。
如果您查看 BottomSheetDialog
,则会看到此方法:
private View wrapInBottomSheet(int layoutResId, View view, ViewGroup.LayoutParams params) {
final CoordinatorLayout coordinator = (CoordinatorLayout) View.inflate(getContext(),
android.support.design.R.layout.design_bottom_sheet_dialog, null);
if (layoutResId != 0 && view == null) {
view = getLayoutInflater().inflate(layoutResId, coordinator, false);
}
FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheet).setBottomSheetCallback(mBottomSheetCallback);
if (params == null) {
bottomSheet.addView(view);
} else {
bottomSheet.addView(view, params);
}
if (shouldWindowCloseOnTouchOutside()) {
final View finalView = view;
coordinator.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (isShowing() &&
MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_UP &&
!coordinator.isPointInChildBounds(finalView,
(int) event.getX(), (int) event.getY())) {
cancel();
return true;
}
return false;
}
});
}
return coordinator;
}
不知道你需要哪种行为,但如果你需要第二种,请按照以下步骤进行:
Create a Java class and extend it from CoordinatorLayout.Behavior<V>
Copy paste code from the default BottomSheetBehavior
file to your new one.
Modify the method clampViewPositionVertical
with the following code:
@Override
public int clampViewPositionVertical(View child, int top, int dy) {
return constrain(top, mMinOffset, mHideable ? mParentHeight : mMaxOffset);
}
int constrain(int amount, int low, int high) {
return amount < low ? low : (amount > high ? high : amount);
}
Add a new state
public static final int STATE_ANCHOR_POINT = X;
Modify the next methods: onLayoutChild
, onStopNestedScroll
, BottomSheetBehavior<V> from(V view)
and setState
(optional)
以下是它的外观
[]