Dynamically change height of BottomSheetBehavior

后端 未结 9 1060
既然无缘
既然无缘 2020-12-13 03:42

I\'m using the BottomSheetBehavior from Google recently released AppCompat v23.2. The height of my bottom sheet depends on the content displayed inside of the b

相关标签:
9条回答
  • 2020-12-13 04:32

    For bottom sheet dialog fragment, read this: Bottom Sheet Dialog Fragment Expand Full Height

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
       super.onActivityCreated(savedInstanceState);
    
       BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
    
       FrameLayout bottomSheet = dialog.findViewById(com.google.android.material.R.id.design_bottom_sheet);
       BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
       behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
       behavior.setPeekHeight(0);
    }
    
    0 讨论(0)
  • 2020-12-13 04:37

    I had the same problem with RelativeLayout as my bottom sheet. The height won't be recalculated. I had to resort to setting the height by the new recalculated value and call BottomSheetBehavior.onLayoutChild.

    This is my temporary solution:

    coordinatorLayout = (CoordinatorLayout)findViewById(R.id.coordinator_layout);
    bottomSheet = findViewById(R.id.bottom_sheet);
    
    int accountHeight = accountTextView.getHeight();
    accountTextView.setVisibility(View.GONE);
    
    bottomSheet.getLayoutParams().height = bottomSheet.getHeight() - accountHeight;
    bottomSheet.requestLayout();
    behavior.onLayoutChild(coordinatorLayout, bottomSheet, ViewCompat.LAYOUT_DIRECTION_LTR);
    
    0 讨论(0)
  • 2020-12-13 04:39

    I've been struggling with a problem similar to yours.

    Manually setting the height of the bottomSheet was the solution for me.

    Having a view viewA that has the BottomSheetBehaviour and a custom method modifyHeight() that modifies the height of the view:

    viewA?.modifyHeight()
    viewA?.measure(
                MeasureSpec.makeMeasureSpec(
                    width,
                    MeasureSpec.EXACTLY
                ),
                MeasureSpec.makeMeasureSpec(
                    0,
                    MeasureSpec.UNSPECIFIED
                )
            )
    val layoutParams = LayoutParams(viewA.measuredWidth, viewA.measuredHeight)
    val bottomSheet = BottomSheetBehavior.from(viewA)
    layoutParams.behavior = bottomSheet
    viewA.layoutParams = layoutParams
    

    My layout would be something like:

        <com.yourpackage.ViewA
        android:id="@+id/viewA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:behavior_peekHeight="50dp"
        app:layout_behavior="@string/bottom_sheet_behavior" />
    

    It is important to reuse the bottomSheetBehaviour of the old layoutParams because it contains the peekHeight and listeners you may have attached.

    0 讨论(0)
提交回复
热议问题