簡體   English   中英

在Android中從上到下打開底部對話框

[英]Open Bottom dialog from mid to top in Android

我希望我的底部對話框默認在中間高度打開,在滑動時,它應該打開到頂部。 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>

我的科特林代碼

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

我嘗試了一些東西,但顯示出這樣的東西

在此處輸入圖片說明

但是我想做這樣的事情

在此處輸入圖片說明

將屬性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">

為您的底頁設置peekHeight應該可以幫助您獲得所需的行為。 這是peekHeight的定義。

STATE_COLLAPSED :底部可見,但僅顯示其窺視高度。 此狀態通常是底表的“靜止位置”。 窺視高度由開發人員選擇,並且應足以指示有更多內容,允許用戶觸發操作或擴展底頁。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM