BottomSheetDialogFragment - How to set expanded height (or min top offset)

后端 未结 5 1676
执笔经年
执笔经年 2020-12-12 18:13

I create a BottomSheetDialogFragment and I want to adjust it\'s maximum expanded height. How can I do that? I can retrieve the BottomSheetBehaviour

5条回答
  •  既然无缘
    2020-12-12 18:47

    BIG UPDATE Avoiding duplicated code I'm giving a link to the full answer in where you can find all the explanation about how to get full behavior like Google Maps.


    I want to adjust it's maximum expanded height. How can I do that?

    Both BottomSheet and BottomSheetDialogFragment use a BottomSheetBehavior that you can found in Support Library 23.x

    That Java class has 2 different uses for mMinOffset, one of them is used to define the area of the parent it will use to draw his content (maybe a NestedScrollView). And the other use if for define the expanded anchor point, I mean, if you slide it up form STATE_COLLAPSEDit will animate your BottomSheetuntil he reached this anchor point BUT if you can still keep sliding up to cover all parent height (CoordiantorLayout Height).

    If you took a look at BottomSheetDialog you will see this method:

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



    No idea which one of those 2 behaviors you want but if you need the second one follow those steps:

    1. Create a Java class and extend it from CoordinatorLayout.Behavior
    2. Copy paste code from default BottomSheetBehavior file to your new one.
    3. 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);
      }
      
    4. Add a new state

      public static final int STATE_ANCHOR_POINT = X;
      
    5. Modify the next methods: onLayoutChild, onStopNestedScroll, BottomSheetBehavior from(V view) and setState (optional)

    And here is how its looks like:
    [CustomBottomSheetBehavior]

提交回复
热议问题