Search code examples
androidandroid-databindingandroid-bottomsheetdialog

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?


Solution

  • 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) {
    
      }
    });