BottomSheetBehavior 不是 CoordinatorLayout 的子项
BottomSheetBehavior is not a child of CoordinatorLayout
这是我的 XML 布局,歌曲列表名称为:
<android.support.design.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/viewA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="@android:color/holo_purple"
android:orientation="horizontal"/>
<android.support.v4.widget.NestedScrollView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="308dp"
/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/personlog"
app:layout_anchor="@id/viewA"
app:layout_anchorGravity="bottom|center"/>
</android.support.design.widget.CoordinatorLayout>
这是我的片段,其中包含此布局:
public class SongList extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.songlist,container,false);
textView=(TextView)view.findViewById(R.id.txt);
View bottomSheet = view.findViewById(R.id.bottom_sheet);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setPeekHeight(200);
return view;}
}
但是午餐时应用程序给我这个错误:
java.lang.IllegalArgumentException: The view is not a child of CoordinatorLayout
来自这一行:
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
如何解决这个问题?似乎一切正常但出现错误...如果有人可以帮助
An interaction behavior plugin for a child view of CoordinatorLayout to make it work as a bottom sheet.
目前你底部 sheet NestedScrollView
是 child 的 LinearLayout
。所以完全放弃 outer-most LinearLayout
。
<android.support.design.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">
<LinearLayout
android:id="@+id/viewA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="@android:color/holo_purple"
android:orientation="horizontal"/>
<android.support.v4.widget.NestedScrollView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="308dp" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/personlog"
app:layout_anchor="@id/viewA"
app:layout_anchorGravity="bottom|center" />
</android.support.design.widget.CoordinatorLayout>
但是现在您在尝试实施底部 sheet 时遇到了更多问题。首先,你不应该在滚动视图中使用 wrap_content
。其次,你不应该在滚动视图中使用列表视图,因为它实现了自己的滚动。您可以通过仅使用列表视图作为底部来简化此操作 sheet.
在我的例子中而不是
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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:layout_width="match_parent"
android:layout_height="match_parent"
>
我使用了 <android.support.constraint.ConstraintLayout
(在包含布局和 BottomSheet 的片段或 activity 中)。
请注意,如果您不使用 CoordinatorLayout 而是使用 BottomSheetDialogFragment(它为您准备了一个),我注意到您无法使用当前版本的 Nav 组件库导航到具有片段方向的对话框片段(2.1.0-alpha05) 并且必须将它实例化为一个新的片段对话框,否则你会得到这个错误,即而不是使用这个:
navController().navigate(MerchantHistoryFragmentDirections.showDateSelection())
你必须使用这个:
fragmentManager?.let {
val dateSelection = DateSelectionFragment.newInstance()
dateSelection.setTargetFragment(this, RC_DATE_SELECTION)
dateSelection.show(it)
}
这是一种迟钝的错误,所以希望这对某人有所帮助。
如果您正在使用数据绑定并将布局包含到片段中,您必须按照以下步骤进行操作
val sheetBehavior = BottomSheetBehavior.from(binding.layoutBottomSheet.root)
您必须在内容布局下方的应用栏布局中设置底部 sheet 布局
就我而言,我使用了以下解决方案来解决问题
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
dialog.setOnShowListener { dialogInterface ->
val bottomSheetDialog = dialogInterface as BottomSheetDialog
setupFullHeight(bottomSheetDialog)
}
return dialog
}
val bottomSheet = bottomSheetDialog
.findViewById<FrameLayout>(R.id.design_bottom_sheet)
val behavior: BottomSheetBehavior<*>?
if (bottomSheet != null) {
behavior = BottomSheetBehavior.from(bottomSheet)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
behavior.isDraggable = false
}
您应该将布局设计更改为
对我有用
这是我的 XML 布局,歌曲列表名称为:
<android.support.design.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/viewA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="@android:color/holo_purple"
android:orientation="horizontal"/>
<android.support.v4.widget.NestedScrollView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="308dp"
/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/personlog"
app:layout_anchor="@id/viewA"
app:layout_anchorGravity="bottom|center"/>
</android.support.design.widget.CoordinatorLayout>
这是我的片段,其中包含此布局:
public class SongList extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.songlist,container,false);
textView=(TextView)view.findViewById(R.id.txt);
View bottomSheet = view.findViewById(R.id.bottom_sheet);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setPeekHeight(200);
return view;}
}
但是午餐时应用程序给我这个错误:
java.lang.IllegalArgumentException: The view is not a child of CoordinatorLayout
来自这一行:
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
如何解决这个问题?似乎一切正常但出现错误...如果有人可以帮助
An interaction behavior plugin for a child view of CoordinatorLayout to make it work as a bottom sheet.
目前你底部 sheet NestedScrollView
是 child 的 LinearLayout
。所以完全放弃 outer-most LinearLayout
。
<android.support.design.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">
<LinearLayout
android:id="@+id/viewA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="@android:color/holo_purple"
android:orientation="horizontal"/>
<android.support.v4.widget.NestedScrollView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="308dp" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/personlog"
app:layout_anchor="@id/viewA"
app:layout_anchorGravity="bottom|center" />
</android.support.design.widget.CoordinatorLayout>
但是现在您在尝试实施底部 sheet 时遇到了更多问题。首先,你不应该在滚动视图中使用 wrap_content
。其次,你不应该在滚动视图中使用列表视图,因为它实现了自己的滚动。您可以通过仅使用列表视图作为底部来简化此操作 sheet.
在我的例子中而不是
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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:layout_width="match_parent"
android:layout_height="match_parent"
>
我使用了 <android.support.constraint.ConstraintLayout
(在包含布局和 BottomSheet 的片段或 activity 中)。
请注意,如果您不使用 CoordinatorLayout 而是使用 BottomSheetDialogFragment(它为您准备了一个),我注意到您无法使用当前版本的 Nav 组件库导航到具有片段方向的对话框片段(2.1.0-alpha05) 并且必须将它实例化为一个新的片段对话框,否则你会得到这个错误,即而不是使用这个:
navController().navigate(MerchantHistoryFragmentDirections.showDateSelection())
你必须使用这个:
fragmentManager?.let {
val dateSelection = DateSelectionFragment.newInstance()
dateSelection.setTargetFragment(this, RC_DATE_SELECTION)
dateSelection.show(it)
}
这是一种迟钝的错误,所以希望这对某人有所帮助。
如果您正在使用数据绑定并将布局包含到片段中,您必须按照以下步骤进行操作
val sheetBehavior = BottomSheetBehavior.from(binding.layoutBottomSheet.root)
您必须在内容布局下方的应用栏布局中设置底部 sheet 布局
就我而言,我使用了以下解决方案来解决问题
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
dialog.setOnShowListener { dialogInterface ->
val bottomSheetDialog = dialogInterface as BottomSheetDialog
setupFullHeight(bottomSheetDialog)
}
return dialog
}
val bottomSheet = bottomSheetDialog
.findViewById<FrameLayout>(R.id.design_bottom_sheet)
val behavior: BottomSheetBehavior<*>?
if (bottomSheet != null) {
behavior = BottomSheetBehavior.from(bottomSheet)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
behavior.isDraggable = false
}
您应该将布局设计更改为
对我有用