BIG CẬP NHẬT Tránh mã trùng lặp Tôi đưa ra một liên kết đến full answer ở nơi bạn có thể tìm thấy tất cả giải thích về cách có được hành vi đầy đủ như Google Maps.
Tôi muốn điều chỉnh chiều cao mở rộng tối đa của nó. Làm thế nào tôi có thể làm điều đó?
Cả BottomSheet
và BottomSheetDialogFragment
sử dụng một BottomSheetBehavior mà bạn có thể tìm thấy trong Thư viện Hỗ trợ 23.x
Đó lớp Java có 2 công dụng khác nhau cho mMinOffset
, một trong số họ được sử dụng để xác định diện tích của cha mẹ nó sẽ sử dụng để vẽ nội dung của anh ấy (có thể là NestedScrollView
). Và việc sử dụng khác nếu để xác định điểm neo mở rộng, ý tôi là, nếu bạn trượt nó lên dạng STATE_COLLAPSED
nó sẽ tạo ảnh động BottomSheet
cho đến khi anh ta đạt đến điểm neo này NHƯNG nếu bạn vẫn có thể trượt lên để che phủ tất cả chiều cao của bố mẹ (Chiều cao điều chỉnh) .
Nếu bạn đã xem xét BottomSheetDialog
bạn sẽ thấy phương pháp này:
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);
}
// We treat the CoordinatorLayout as outside the dialog though it is technically inside
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;
}
Không có ý tưởng mà một trong những 2 hành vi mà bạn muốn nhưng nếu bạn cần một giây làm theo những bước :
- Tạo một lớp Java và mở rộng từ
CoordinatorLayout.Behavior<V>
- Sao chép mã dán từ mặc định
BottomSheetBehavior
tệp mới của bạn.
Sửa đổi phương pháp clampViewPositionVertical
với đoạn mã sau:
@ Override public int clampViewPositionVertical (Xem đứa trẻ, int top, int dy) { trở Constrain (hàng đầu, mMinOffset, mHideable mParentHeight: mMaxOffset); } int constrain (int số tiền, int thấp, int cao) { trả lại số tiền < thấp? thấp: (số tiền> cao?cao: số tiền); }
Thêm một trạng thái mới
public static int thức STATE_ANCHOR_POINT = X;
Sửa đổi phương pháp tiếp theo: onLayoutChild
, onStopNestedScroll
, BottomSheetBehavior<V> from(V view)
và setState
(không bắt buộc)
Và đây là cách nhìn của nó như:
[]
bạn có tìm thấy giải pháp chưa? – abat
Tôi muốn biết rằng ** làm thế nào tôi có thể thiết lập 'max height' cho' modal bottom sheet'' **. Tôi cần tờ dưới cùng phương thức của tôi để đi đến nửa màn hình và không bao giờ cho đến đầu thậm chí sau khi kéo ... Bất kỳ manh mối nào? – eRaisedToX