首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kotlin智能角色转换为“BottomSheetBehavior应用程序崩溃”

Kotlin智能角色转换为“BottomSheetBehavior应用程序崩溃”
EN

Stack Overflow用户
提问于 2020-04-07 22:31:16
回答 2查看 2.3K关注 0票数 1

我这样做:

代码语言:javascript
复制
private fun initComponent() {
        // get the bottom sheet view
        val llBottomSheet = findViewById<View>(R.id.bottom_sheet) as LinearLayout

        // init the bottom sheet behavior
        bottomSheetBehavior = BottomSheetBehavior.from(llBottomSheet)

        // change the state of the bottom sheet
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED)

        // set callback for changes
        bottomSheetBehavior.setBottomSheetCallback(object : BottomSheetCallback() {
            override fun onStateChanged(bottomSheet: View, newState: Int) {}
            override fun onSlide(bottomSheet: View, slideOffset: Float) {}
        })

}

当我启动我的应用程序时,我的应用程序会崩溃:

代码语言:javascript
复制
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED)

这是一个日志:

代码语言:javascript
复制
Smart cast to 'BottomSheetBehavior<LinearLayout!>!' is impossible, because 'bottomSheetBehavior' is a mutable property that could have been changed by this time

我不知道该怎么修复这个.

这是我在我的项目中得到的xml文件:

代码语言:javascript
复制
  <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:orientation="vertical"
    app:behavior_hideable="true"
    app:behavior_peekHeight="250dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        app:cardCornerRadius="1dp"
        app:cardElevation="20dp">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/spacing_xxlarge"
                android:layout_marginStart="@dimen/spacing_xxlarge"
                android:gravity="center_vertical"
                android:text="Dandelion Chocolate"
                android:textAppearance="@style/TextAppearance.AppCompat.Headline" />
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

我尝试将ConstraintLayout更改为LinearLayout,但没有帮助。不过,当我打开它时,我的应用程序还是崩溃了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-07 23:04:26

在第一步中,您必须更改根视图(在布局中):

代码语言:javascript
复制
<androidx.constraintlayout.widget.ConstraintLayout

至:

代码语言:javascript
复制
<androidx.coordinatorlayout.widget.CoordinatorLayout

因为BottomSheet的行为需要CoordinatorLayout

在下一步中,您必须修改布局:

CardView

  • move

  • app:layout_behavior从根移动到app:id,从根移动到CardView

在最后一步中,从以下位置修改布局的铸造:

代码语言:javascript
复制
val llBottomSheet = findViewById<View>(R.id.bottom_sheet) as LinearLayout

代码语言:javascript
复制
val llBottomSheet = findViewById<CardView>(R.id.bottom_sheet)

在布局中有CardView,因此强制转换as LinearLayout是不正确的。

完整的工作示例:

代码:

代码语言:javascript
复制
private fun initComponent() {
    // get the bottom sheet view
    val llBottomSheet = findViewById<CardView>(R.id.bottom_sheet)

    // init the bottom sheet behavior
    val bottomSheetBehavior = BottomSheetBehavior.from(llBottomSheet)

    // change the state of the bottom sheet
    bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED

    // set callback for changes
    bottomSheetBehavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
        override fun onStateChanged(bottomSheet: View, newState: Int) {}
        override fun onSlide(bottomSheet: View, slideOffset: Float) {}
    })
}

布局:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:orientation="vertical"
    app:behavior_hideable="true"
    app:behavior_peekHeight="250dp">

    <androidx.cardview.widget.CardView
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        app:cardCornerRadius="1dp"
        app:cardElevation="20dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:text="Dandelion Chocolate"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline" />
    </androidx.cardview.widget.CardView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
票数 4
EN

Stack Overflow用户

发布于 2020-04-07 23:03:25

您只能在com.google.android.material.bottomsheet.BottomSheetBehavior的子代中使用CoordinatorLayout

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61090448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档