Android 如何设置底部板材的半展开状态

Android 如何设置底部板材的半展开状态,android,material-design,bottom-sheet,Android,Material Design,Bottom Sheet,我有底片的布局 <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/coordinator_layout" android:layout_width="matc

我有底片的布局

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="@color/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed" />
    </com.google.android.material.appbar.AppBarLayout>


    <include layout="@layout/content_main_weather_map" />

    <include layout="@layout/bottom_sheet" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

底页布局

 <?xml version="1.0" encoding="utf-8"?>
    <androidx.core.widget.NestedScrollView 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:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:clipToPadding="true"
        app:behavior_peekHeight="80dp"
        app:layout_behavior="@string/bottom_sheet_behavior">

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

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/weather_recycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                tools:listitem="@layout/item_weather" />

        </LinearLayout>


    </androidx.core.widget.NestedScrollView>

对我来说,我的下半页打开是必要的,在重新拖动后,它会打开到全屏。这是如何在谷歌地图应用程序中完成的。但是我不知道怎么做。

只需将BottomSheetBehavior状态设置为BottomSheetBehavior.state\u HALF\u展开即可。 另外,如果您需要在完全展开后让用户再次返回到半展开模式,则需要将peek height设置为窗口高度的一半

val bottomSheetBehavior = BottomSheetBehavior.from<NestedScrollView>(bottom_sheet)
val metrics = resources.displayMetrics
bottomSheetBehavior.peekHeight = metrics.heightPixels / 2
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
val bottomSheetBehavior=bottomSheetBehavior.from(底部工作表)
val metrics=resources.displayMetrics
bottomSheetBehavior.Peek高度=metrics.heightPixels/2
bottomSheetBehavior.state=bottomSheetBehavior.state\u展开一半

虽然这个问题已经得到了回答,但只是找到了另一种方式来实现这种行为,以便为他人共享

创建一个全局变量,并使用行为的默认状态初始化它,如

然后,在BottomSheetBehavior.BottomSheetCallback中将状态变量更新为当前状态

而且 BottomSheetBehavior.STATE_拖动,如果状态未展开一半

将状态设置为BottomSheetBehavior。状态已展开

这将使您的底板采取三个步骤,即折叠、半展开和展开

希望它能帮助别人

我尝试了和,但与我想要的行为不匹配

使用kotlin中的以下解决方案,如果底板滑动接近展开状态,它将保持展开状态,如果接近半状态,它将保持在半状态,如果接近折叠,它将保持折叠状态:

    val bottomSheet = view.findViewById<View>(R.id.bottom_sheet1)
    val mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
    mBottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
    mBottomSheetBehavior.addBottomSheetCallback(object: BottomSheetBehavior.BottomSheetCallback(){
        override fun onStateChanged(bottomSheet: View, newState: Int) {
        }

        override fun onSlide(bottomSheet: View, slideOffset: Float) {
            val upperState = 0.66
            val lowerState = 0.33
            if (bottomSheetEventsFilterBehavior.state == BottomSheetBehavior.STATE_SETTLING ) {
                if(slideOffset >= upperState){
                    mBottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
                }
                if(slideOffset > lowerState && slideOffset < upperState){
                    mBottomSheetBehavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
                }
                if(slideOffset <= lowerState){
                    mBottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
                }
            }
        }
    })
val bottomSheet=view.findViewById(R.id.bottom_sheet1)
val mBottomSheetBehavior=底部板材行为。从(底部板材)
mBottomSheetBehavior.state=底部挡板行为.state\u已折叠
mBottomSheetBehavior.addBottomSheetCallback(对象:BottomSheetBehavior.BottomSheetCallback()){
覆盖状态更改(底部工作表:视图,新闻状态:Int){
}
覆盖滑轨上的乐趣(底部工作表:视图,滑轨偏移:浮动){
val upperState=0.66
val lowerState=0.33
if(bottomSheetEventsFilterBehavior.state==BottomSheetBehavior.state\u){
如果(slideOffset>=upperState){
mBottomSheetBehavior.state=BottomSheetBehavior.state\u已展开
}
if(slideOffset>lowerState&&slideOffset如果(slideOffset,最好充分利用框架的潜力。正如官方文件所述,方法:

设置展开图纸的高度是由其内容的高度决定的,还是分两个阶段展开(父级高度的一半) 容器,父容器的完整高度)。默认值为true

因此,您只需使用以下命令将
setFitToContent
设置为
false

behavior = BottomSheetBehavior.from(your_bottom_sheet_xml)
behavior.isFitToContents = false
behavior.halfExpandedRatio = 0.6f
有了这三行代码,底片将首先扩展到屏幕的60%,然后完全扩展到100%


希望能有帮助!

onSlide呢?我希望中途位置被视为卡已经滑到顶部。但我看到的情况是,onSlide偏移量仍然占总屏幕的百分比。halfxpandedRatio?@ArnoldBrown在1.1.0-Alpha05MA日内提供!!感谢您的见解这打破了fling行为orThanks@Kochchchy,我同意,我推荐这种解决方案
class BottomSheetFragment : BottomSheetDialogFragment() {
    /* inside of your Bottom Sheet Dialog Fragment */
    override fun onStart() {
        super.onStart()
        BottomSheetBehavior.from(requireView().parent as View).apply {
        state = BottomSheetBehavior.STATE_HALF_EXPANDED
       }
    }
}
behavior = BottomSheetBehavior.from(your_bottom_sheet_xml)
behavior.isFitToContents = false
behavior.halfExpandedRatio = 0.6f
class BottomSheetFragment : BottomSheetDialogFragment() {
    /* inside of your Bottom Sheet Dialog Fragment */
    override fun onStart() {
        super.onStart()
        BottomSheetBehavior.from(requireView().parent as View).apply {
        state = BottomSheetBehavior.STATE_HALF_EXPANDED
       }
    }
}