android-fragments 仅在一个窗格中显示BottomSheetDialogFragment

lvjbypge  于 2022-11-14  发布在  Android
关注(0)|答案(2)|浏览(200)

我正在尝试在多窗格应用程序中实现一个BottomSheetDialogFragment。它现在看起来是这样的:

----------------------------------------
|                        |             |
|                        |             |
|      Fragment          | Fragment    |
|                        |             |
|                        |             |
----------------------------------------
|       BottomSheetDialogFragment      |
----------------------------------------

但我想实现的是:

----------------------------------------
|                        |             |
|                        |             |
|      Fragment          | Fragment    |
|                        |             |
|                        |             |
|------------------------|             |
|                        |             |
|    BottomSheetDialog   |             |
----------------------------------------

我的问题是:可能用BottomSheetDialogFragment来实现吗?或者我需要一个不同的方法?如果可能的话,你们能为我指出如何实现这个目标的方向吗?谢谢!

vmpqdwk3

vmpqdwk31#

当我在开发一个带有底部表单的平板电脑解决方案时,我遇到了与您相同的问题。对我来说,解决方案是在Fragment 1中添加一个子片段。该子片段是一个普通的Fragment,使用BottomSheetBehaviour类来建立与BottomSheetDialogFragment相同的结果。

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/bottom_sheet_wrapper"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#A6000000">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/bottom_sheet_dialog"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
        </androidx.coordinatorlayout.widget.CoordinatorLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>

请务必将BottomSheetBehaviour视图放置在CoordinatorLayout中。
然后将下面的Kotlin代码放到你的代码片段中(在这个例子中,我使用了带有绑定的布局)。

onCreateView() {
   ...
   val bottomSheetBehaviour : BottomSheetBehavior<ConstraintLayout> = BottomSheetBehavior.from(binding.bottomSheetDialog)
   bottomSheetBehaviour.state = BottomSheetBehavior.STATE_EXPANDED
   ...
}

然后,您可以使用bottomSheetBehaviour.addBottomSheetCallback为底部表单添加状态和幻灯片更改侦听器。
我从这一页找到了灵感:https://www.androidhive.info/2017/12/android-working-with-bottom-sheet/

nkkqxpd9

nkkqxpd92#

您可以尝试使用RelativeLayout来实现您想要的功能:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/fragment_container_1"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_dialog"/>

    <FrameLayout
        android:id="@+id/bottom_dialog"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_alignLeft="@+id/fragment_container_1"
        android:layout_alignRight="@+id/fragment_container_1"
        android:layout_alignParentBottom="true"/>

    <FrameLayout
        android:id="@+id/fragment_container_2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@+id/fragment_container_1"/>

</RelativeLayout>

相关问题