简体   繁体   English

Android - 数据绑定找不到方法,使用 BottomSheetBehavior

[英]Android - Data Binding cannot find method, using BottomSheetBehavior

Example Code示例代码

<?xml version="1.0" encoding="utf-8"?>
    <layout 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">
    
        <data>
            <import type="com.google.android.material.bottomsheet.BottomSheetBehavior" />
        </data>
...

Then, if I use it BottomSheetBehavior with the code below,然后,如果我在下面的代码中使用它 BottomSheetBehavior,

<androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background='@{BottomSheetBehavior.from(bottomSheet).state == BottomSheetBehavior.STATE_EXPANDED ? @color/black : @color/white}'
            android:minHeight="300dp">

This code throws the error此代码引发错误

cannot find method from(com.app.abee.databinding.LayoutBottomSheetBinding) in class com.google.android.material.bottomsheet.BottomSheetBehavior

Why is it?为什么?

I think this code may not executed properly.我认为这段代码可能无法正确执行。 because BottomSheetBehavior's state return value is not Observable.因为 BottomSheetBehavior 的状态返回值不是 Observable。

So, is there anyone who can explain meaning of this error?那么,有没有人可以解释这个错误的含义?

You can listen bottom sheet behaviour state using call back... Not in binding... Use callback and update your views based on state您可以使用回调收听底部工作表行为状态...不在绑定中...使用回调并根据状态更新您的视图

bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
  @Override public void onStateChanged(@NonNull View bottomSheet, int newState) {
    Log.d(TAG, "onStateChanged: " + newState);
  }

  @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) {

  }
});

Now, I've solved it This error is due to the use of the tag.现在,我已经解决了这个错误是由于使用了标签。

<include
            android:id="@+id/bottomSheet"
            layout="@layout/layout_bottom_sheet" />

The included layout does not appear to be accessible from the data binding.包含的布局似乎无法从数据绑定中访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM