在Android中从上到下打开底部对话框

时间:2019-06-11 11:03:15

标签: android kotlin

我希望我的底部对话框默认在中间高度打开,而在滑动时,它应该打开到顶部。 Google地图将在上半部分显示,而另一半将在Bottomsheet中显示,并且当用户向上滑动底部时,它应该向顶部打开。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:orientation="vertical"
             android:layout_height="match_parent"
             tools:context=".home.MainActivity">

<LinearLayout android:layout_width="match_parent"
              android:orientation="vertical"
              android:weightSum="2"
              android:layout_height="match_parent">

    <fragment
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/map"
            android:layout_weight="1"
            tools:context=".module.ride.activity.MapsActivity"
            android:name="com.google.android.gms.maps.SupportMapFragment"/>

    <LinearLayout android:layout_width="match_parent"
                  android:orientation="vertical"
                  android:layout_weight="1"
                  android:layout_height="0dp">

    </LinearLayout>

</LinearLayout>


<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            android:orientation="vertical"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center_vertical"
                android:weightSum="3">

            <ImageView android:layout_width="match_parent"
                       app:srcCompat="@drawable/ic_openclose"
                       android:id="@+id/imgExpand"
                       android:layout_height="@dimen/_40sdp"/>

        </LinearLayout>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Chicken Fried Rice 1x1"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Paneer Tikka 1x2"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Delivery Address"
                android:textColor="#444"
                android:textStyle="bold"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Flat No 404, Skyline Apartments, Vizag - 500576"/>

        <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:background="#000"
                android:foreground="?attr/selectableItemBackground"
                android:text="PROCEED PAYMENT"
                android:textColor="#fff"/>

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>
</FrameLayout>

我的Kotlin代码

 var sheetBehavior: BottomSheetBehavior<*>? = null
 sheetBehavior = BottomSheetBehavior.from(bottom_sheet);
        (sheetBehavior as BottomSheetBehavior<*>?)!!.setBottomSheetCallback(object :
            BottomSheetBehavior.BottomSheetCallback() {
            override fun onSlide(p0: View, p1: Float) {

        }
        override fun onStateChanged(p0: View, p1: Int) {
            when (p0.id) {

                BottomSheetBehavior.STATE_HIDDEN -> {
                    System.out.println("----------STATE_HIDDEN")
                }
                BottomSheetBehavior.STATE_EXPANDED -> {
                    System.out.println("-------------STATE_EXPANDED")
                }
                BottomSheetBehavior.STATE_COLLAPSED -> {
                    System.out.println("-----------STATE_COLLAPSED")
                }
            }
        }
    })
    imgExpand.setOnClickListener() {
        if ((sheetBehavior as BottomSheetBehavior<*>?)!!.state != BottomSheetBehavior.STATE_EXPANDED) {
            (sheetBehavior as BottomSheetBehavior<*>?)!!.setState(BottomSheetBehavior.STATE_EXPANDED);
        } else {
            (sheetBehavior as BottomSheetBehavior<*>?)!!.setState(BottomSheetBehavior.STATE_COLLAPSED);
        }
    }
}

我尝试了一些东西,但显示出类似这样的内容

enter image description here

但是我想做这样的事情

enter image description here

2 个答案:

答案 0 :(得分:1)

将属性app:layout_behavior="android.support.design.widget.BottomSheetBehavior">LinearLayout移到工作表的根视图CoordinatorLayout

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

<LinearLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:orientation="vertical">

答案 1 :(得分:1)

为底表设置peekHeight应该可以帮助您获得所需的行为。这是peekHeight的定义。

  

STATE_COLLAPSED :底部可见,但仅显示其窥视高度。此状态通常是底表的“静止位置”。窥视高度由开发人员选择,应足以指示有更多内容,允许用户触发操作或扩展底页。