【问题标题】:BottomSheetBehavior is not a child of CoordinatorLayoutBottomSheetBehavior 不是 CoordinatorLayout 的子项
【发布时间】:2017-09-03 17:51:50
【问题描述】:

这是我的 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);

如何解决这个问题?似乎一切正常,但给出错误...如果有人可以请帮助

【问题讨论】:

  • 那么如何才能真正做到这种布局呢?如果你有一些解决方案请回答
  • 我在 CoordinatorLayout 中添加了 NestedScrollView 但还是同样的问题
  • 能理解你的意思。如果您想帮助我,请回答我的问题而不是评论,然后告诉我必须将代码更改为什么或类似的东西。 tnx

标签: android android-coordinatorlayout


【解决方案1】:

BottomSheetBehavior

一个用于 CoordinatorLayout 子视图的交互行为插件,使其可以作为底部工作表。

目前,您的底部工作表NestedScrollViewLinearLayout 的孩子。因此,只需完全删除最外层的 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>

但是现在您尝试实施的底部工作表存在更多问题。首先,您不应该将wrap_content 与滚动视图一起使用。其次,您不应该在滚动视图中使用列表视图,因为它正在实现自己的滚动。您可能可以通过仅将列表视图用作底部工作表来简化此操作。

【讨论】:

  • 还有一个包含单个列表视图的线性布局。
【解决方案2】:

如果您使用数据绑定并将布局包含到片段中,您必须执行以下操作

val sheetBehavior = BottomSheetBehavior.from(binding.layoutBottomSheet.root)

【讨论】:

    【解决方案3】:

    注意,如果您不使用 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)
            }
    

    这是一种迟钝的错误,所以希望这对某人有所帮助。

    【讨论】:

      【解决方案4】:

      在我的情况下,而不是

      <?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"
          >
      

      我使用了&lt;android.support.constraint.ConstraintLayout(在包含布局和 BottomSheet 的片段或活动中)。

      【讨论】:

        【解决方案5】:

        您必须在内容布局下方的 appbar 布局中设置底部表单布局

        【讨论】:

          【解决方案6】:

          就我而言,我使用以下解决方案来解决问题

              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
              }
          

          【讨论】:

            猜你喜欢
            • 2018-09-10
            • 2016-12-19
            • 2016-07-13
            • 1970-01-01
            • 2015-10-26
            • 1970-01-01
            • 1970-01-01
            • 2018-06-03
            • 1970-01-01
            相关资源
            最近更新 更多