我这样做:
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) {}
})
}当我启动我的应用程序时,我的应用程序会崩溃:
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED)这是一个日志:
Smart cast to 'BottomSheetBehavior<LinearLayout!>!' is impossible, because 'bottomSheetBehavior' is a mutable property that could have been changed by this time我不知道该怎么修复这个.
这是我在我的项目中得到的xml文件:
<?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,但没有帮助。不过,当我打开它时,我的应用程序还是崩溃了。
发布于 2020-04-07 23:04:26
在第一步中,您必须更改根视图(在布局中):
<androidx.constraintlayout.widget.ConstraintLayout至:
<androidx.coordinatorlayout.widget.CoordinatorLayout因为BottomSheet的行为需要CoordinatorLayout。
在下一步中,您必须修改布局:
CardView
app:layout_behavior从根移动到app:id,从根移动到CardView在最后一步中,从以下位置修改布局的铸造:
val llBottomSheet = findViewById<View>(R.id.bottom_sheet) as LinearLayout至
val llBottomSheet = findViewById<CardView>(R.id.bottom_sheet)在布局中有CardView,因此强制转换as LinearLayout是不正确的。
完整的工作示例:
代码:
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) {}
})
}布局:
<?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>发布于 2020-04-07 23:03:25
您只能在com.google.android.material.bottomsheet.BottomSheetBehavior的子代中使用CoordinatorLayout。
https://stackoverflow.com/questions/61090448
复制相似问题