26

Tôi tạo một BottomSheetDialogFragment và 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 đó? Tôi có thể truy xuất số BottomSheetBehaviour nhưng tất cả những gì tôi có thể tìm thấy là thiết lập cho chiều cao nhìn trộm nhưng không có gì cho chiều cao được mở rộng.BottomSheetDialogFragment - Cách thiết lập chiều cao được mở rộng (hoặc tối thiểu bù đắp)

public class DialogMediaDetails extends BottomSheetDialogFragment 
{ 
    @Override 
    public void setupDialog(Dialog dialog, int style) 
    { 
     super.setupDialog(dialog, style); 
     View view = View.inflate(getContext(), R.layout.dialog_media_details, null); 
     dialog.setContentView(view); 

     ... 

     View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet); 
     BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet); 
     behavior.setPeekHeight(...); 
     // how to set maximum expanded height???? Or a minimum top offset? 

    } 
} 

EDIT

Tại sao tôi cần điều đó? Bởi vì tôi thấy một Dialog BottomSheet vào một hoạt động toàn màn hình và có vẻ xấu nếu BottomSheet để lại một khoảng trống trên đầu ...

+0

bạn có tìm thấy giải pháp chưa? – abat

+0

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

Trả lời

5

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ả BottomSheetBottomSheetDialogFragment 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 :

  1. Tạo một lớp Java và mở rộng từ CoordinatorLayout.Behavior<V>
  2. Sao chép mã dán từ mặc định BottomSheetBehavior tệp mới của bạn.
  3. 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); }

  4. Thêm một trạng thái mới

    public static int thức STATE_ANCHOR_POINT = X;

  5. Sửa đổi phương pháp tiếp theo: onLayoutChild, onStopNestedScroll, BottomSheetBehavior<V> from(V view)setState (không bắt buộc)

Và đây là cách nhìn của nó như:
[CustomBottomSheetBehavior]

4

Tôi tìm thấy một nhiều câu trả lời đơn giản hơn; trong ví dụ của bạn, nơi bạn có được những FrameLayout cho tấm đáy sử dụng mã này

View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet); 

sau đó bạn có thể thiết lập chiều cao trên params bố trí cho rằng xem để bất cứ điều gì chiều cao bạn muốn thiết lập chiều cao mở rộng ra.

bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT; 
3

Chiều cao đang được bao bọc vì chế độ xem tăng cao được thêm vào FrameLayout có layout_height=wrap_content. Xem FrameLayout (R.id.design_bottom_sheet) tại https://github.com/dandar3/android-support-design/blob/master/res/layout/design_bottom_sheet_dialog.xml.

Lớp bên dưới làm cho trang tính toàn màn hình dưới cùng, nền trong suốt và mở rộng hoàn toàn lên trên cùng.

public class FullScreenBottomSheetDialogFragment extends BottomSheetDialogFragment { 


    @CallSuper 
    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     ButterKnife.bind(this, view); 
    } 


    @Override 
    public void onStart() { 
     super.onStart(); 
     Dialog dialog = getDialog(); 

     if (dialog != null) { 
      View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet); 
      bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT; 
     } 
     View view = getView(); 
     view.post(() -> { 
      View parent = (View) view.getParent(); 
      CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams(); 
      CoordinatorLayout.Behavior behavior = params.getBehavior(); 
      BottomSheetBehavior bottomSheetBehavior = (BottomSheetBehavior) behavior; 
      bottomSheetBehavior.setPeekHeight(view.getMeasuredHeight()); 

      parent.setBackgroundColor(Color.TRANSPARENT); 
     }); 
    } 

} 
Các vấn đề liên quan