Android BottomSheetDialogFragment无法完全展开

35
我有以下测试底部表单实现。当我将peekHeight设置为小于500的值时,它可以工作。但是在某个值之后,无论增加多少peek高度都不会改变底部表单的扩展方式,只能手动拖动。如何以编程方式设置peekHeight,以确保底部表单自动扩展到peek高度。 enter image description here bottom_sheet_dialog_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/locUXCoordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/locUXView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        app:behavior_hideable="false"
        app:behavior_peekHeight="0dp"
        app:layout_behavior="@string/bottom_sheet_behavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="1 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="2 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="3 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="4 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="5 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="6 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="7 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="8 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="9 Value" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="First Value" />
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

Java代码

public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment {

    private static BottomSheetBehavior bottomSheetBehavior;
    private static View bottomSheetInternal;
    private static MyBottomSheetDialogFragment INSTANCE;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                BottomSheetDialog d = (BottomSheetDialog) dialog;
                CoordinatorLayout coordinatorLayout = (CoordinatorLayout)d.findViewById(R.id.locUXCoordinatorLayout);
                bottomSheetInternal = d.findViewById(R.id.locUXView);
                bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetInternal);
                bottomSheetBehavior.setPeekHeight(bottomSheetInternal.getHeight());
                bottomSheetInternal.requestLayout();
                coordinatorLayout.getLayoutParams().height = bottomSheetInternal.getHeight();
                Toast.makeText(getActivity(), "Height is" + bottomSheetInternal.getHeight() + "  " + coordinatorLayout.getLayoutParams().height, Toast.LENGTH_LONG).show();

            }
        });
        INSTANCE = this;
        return inflater.inflate(R.layout.bottom_sheet_dialog_main, container, false);
    }
}

1
你应该意识到,"窥视高度"是指被"折叠"的表格的高度,只显示其中一部分。如果将"窥视高度"设置为表格本身的高度(或更高)...那么整个"窥视"就没有意义了。你能否解释一下你想要做什么? - David Medenjak
谢谢。是的,我意识到 peekHeight 是为折叠视图设置的。无论如何,我已经成功纠正了这个问题。 - Nandish A
这一行代码对我很有帮助: coordinatorLayout.getLayoutParams().height = bottomSheetInternal.getHeight(); - Andrii Kovalchuk
11个回答

0
通过像这样使用BottomSheetCallback:
class MyBottomSheetDialogFragment() : BottomSheetCallback() { 
    private val bottomSheetBehavior: BottomSheetBehavior<ConstraintLayout>

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        bottomSheetBehavior = BottomSheetBehavior.from(binding.bottomSheet)
        bottomSheetBehavior.addBottomSheetCallback(this)
    }

    override fun onStateChanged(bottomSheet: View, newState: Int) {
        val slideOffset: Float = bottomSheetBehavior.calculateSlideOffset()
        if (newState == STATE_EXPANDED && slideOffset < 1f) {
            // Enforce to expand the bottom sheet fully.
            bottomSheetBehavior.halfExpandedRatio = slideOffset
            bottomSheetBehavior.state = STATE_HALF_EXPANDED
            bottomSheetBehavior.state = STATE_EXPANDED
        }
    }

    override fun onSlide(bottomSheet: View, slideOffset: Float) {
        if (slideOffset > 1f) {
            // Reduce the height of the expanded bottom sheet.
            bottomSheetBehavior.halfExpandedRatio = BOTTOM_SHEET_SLIDE_OFFSET_ALMOST_FULL
            bottomSheetBehavior.state = STATE_HALF_EXPANDED
            bottomSheetBehavior.state = STATE_EXPANDED
        }
    }

    companion object {
        private const val BOTTOM_SHEET_SLIDE_OFFSET_ALMOST_FULL = 0.9f
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接