简体   繁体   English

数据绑定不起作用 Android Studio 4.1

[英]DataBinding is not working Android Studio 4.1

Am using Android Studio 4.1 and Gradle gradle-6.5-all with this am not able to use DataBinding.我正在使用 Android Studio 4.1 和 Gradle gradle-6.5-all,但无法使用 DataBinding。

SDK Versions SDK 版本

compileSdkVersion 30
buildToolsVersion "30.0.2"

minSdkVersion 16
targetSdkVersion 30

Below is my XML, BindingAdapter and build.gradle code.下面是我的 XML、BindingAdapter 和 build.gradle 代码。

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    id 'kotlin-kapt'
    id "androidx.navigation.safeargs.kotlin"
}

android {
    ....
    buildFeatures {
         viewBinding true
         dataBinding true
    }
}
<?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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/stepText"
            android:layout_width="40dp"
            android:layout_height="40dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:stepBackground="@{1}"
            android:text="1"
            android:textAllCaps="true"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            android:padding="20dp"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
@BindingAdapter("stepBackground")
fun stepBackground(view: View, type: Int?) {
    Timber.e("stepBackground ${view.background}")
    type?.let {
        when(it) {
            1 -> view.setBackgroundResource(R.drawable.white_shape_circle)
            2 -> view.setBackgroundResource(R.drawable.red_shape_circle)
            else -> view.setBackgroundResource(R.drawable.grey_shape_circle)
        }
    }
}

In above code am trying to apply dynamic background based on Step Count.. But here it's not applying the Background.在上面的代码中,我试图根据步数应用动态背景。但这里没有应用背景。 I have also put breakpoint to inspect whether BindingAdapter method is calling or not.我还设置了断点来检查 BindingAdapter 方法是否正在调用。 But Debugger is not going to BindingAdapter method.但是Debugger 不会去BindingAdapter 方法。

May I know what is missing here?我可以知道这里缺少什么吗?

Add following lines in app-level build Gradle and then clean project->Rebuild Project.在应用程序级构建 Gradle 中添加以下行,然后清理项目->重建项目。 It works for me这个对我有用

 dataBinding {
    enabled = true
}

kapt {
    generateStubs = true
}

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

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