我试图收听底部表中的滑动和状态更改,但出现错误:视图不是 CoordinatorLayout 的子视图。stackoverflow 上已经有很少的资源,但它们似乎没有解决我的问题,所以用我的代码发布这个新的。
我添加了一个回调来监控底部表中的滑动和状态变化。
private lateinit var bottomSheetBehavior: BottomSheetBehavior<View>
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val layoutId = R.layout.bottomsheet_series_summary
bottomSheetBehavior = BottomSheetBehavior.from(inflater.inflate(layoutId, container, false))
bottomSheetBehavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, slideOffset: Float) {
}
override fun onStateChanged(bottomSheet: View, newState: Int) {
}
})
return inflater.inflate(layoutId, container, false)
}
xml代码是:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/f_s_s"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_profile_header"
tools:context=".ui.main.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="vertical"
android:background="@drawable/gradient_profile_header">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="225dp"
android:theme="@style/AppTheme.AppBarOverlay"
android:background="@drawable/gradient_profile_header">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:collapsedTitleTextAppearance="@style/PC.TextView.SeriesTitle"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="180dp"
app:expandedTitleMarginStart="@dimen/activity_horizontal_margin"
app:expandedTitleTextAppearance="@style/PC.TextView.SeriesTitle"
app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlwaysCollapsed"
app:toolbarId="@id/toolbar">
........
........
I have combination of linearlayout , constrain layout and frame layout.
........
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我得到的错误是:
java.lang.IllegalArgumentException: The view is not a child of CoordinatorLayout
E at com.google.android.material.bottomsheet.BottomSheetBehavior.from(BottomSheetBehavior.java:1599)
E at com.pratilipi.comics.ui.misc.bottomSheet.SeriesSummaryBottomSheet.onCreateView(SeriesSummaryBottomSheet.kt:52)
E at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
E at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
E at android.app.ActivityThread.main(ActivityThread.java:7592)
E at java.lang.reflect.Method.invoke(Native Method)
E at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
E at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
OOMEventManagerFK D checkEventAndDumpForJE: 0
java.lang.Exception: Thread.setDefaultUncaughtExceptionHandler
E
.......
.......
.......
```