简体   繁体   中英

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,

<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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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