android - Kotlin 项目中的数据绑定
问题描述
附录:
我意识到问题与:
app:visibleGone="@{isLoaded}"
在以下布局中:
<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>
<variable
name="isLoaded"
type="boolean" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:visibleGone="@{isLoaded}" />
</android.support.v4.widget.SwipeRefreshLayout>
<include
layout="@layout/network_state_item"
app:visibleGone="@{!isLoaded}" />
</FrameLayout>
</layout>
我在 Bitbucket 中有以下分支:https ://bitbucket.org/ali-rezaei/tmdb/src/dataBinding/
Kotlin compiler
构建项目时出现以下错误:
e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
如果您能帮助我,我将不胜感激。
这是我的项目 build.gradle :
buildscript {
ext.kotlin_version = '1.2.60'
ext.gradle_version = '3.1.4'
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:$gradle_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
这是我的应用程序 build.gradle :
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
androidExtensions {
experimental = true
}
android {
compileSdkVersion rootProject.ext.compileSdkVersion
defaultConfig {
applicationId "com.sample.android.tmdb"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
dataBinding {
enabled = true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// Support libraries
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
implementation "com.android.support:design:$rootProject.supportLibraryVersion"
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
// Architecture components
implementation "android.arch.paging:runtime:$rootProject.paging"
implementation "android.arch.lifecycle:runtime:$rootProject.lifecycle"
implementation "android.arch.lifecycle:extensions:$rootProject.lifecycle"
//DataBinding
kapt "com.android.databinding:compiler:$gradle_version"
//Gson
implementation 'com.google.code.gson:gson:2.8.2'
//Retrofit
implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofitVersion"
implementation "com.squareup.retrofit2:adapter-rxjava2:$rootProject.retrofitVersion"
implementation "com.squareup.retrofit2:converter-gson:$rootProject.retrofitVersion"
//Dagger
implementation "com.google.dagger:dagger:$rootProject.daggerVersion"
kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
compileOnly 'org.glassfish:javax.annotation:10.0-b28'
implementation "com.google.dagger:dagger-android:$rootProject.daggerVersion"
implementation "com.google.dagger:dagger-android-support:$rootProject.daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
// Network
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
implementation "com.github.bumptech.glide:glide:$rootProject.glideVersion"
kapt "com.github.bumptech.glide:compiler:$rootProject.glideVersion"
//Butter knife
implementation "com.jakewharton:butterknife:$rootProject.butterknifeVersion"
kapt "com.jakewharton:butterknife-compiler:$rootProject.butterknifeVersion"
//Timber
implementation 'com.jakewharton.timber:timber:4.5.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
作为示例,您可以查看:https ://www.moveoapps.com/blog/how-to-use-data-binding-library-with-kotlin-a-step-by-step-guide/
解决方案
问题将通过在项目中创建以下方法作为 Kotlin 文件来解决:
@BindingAdapter("visibleGone")
fun View.visibleGone(visible: Boolean) {
visibility = if (visible) View.VISIBLE else View.GONE
}
推荐阅读
- .net - rdlc 报告 - 不显示 DataSet 的新数据
- r - 关于更改数据集中的值遇到的问题
- c# - .NET Core 2.1 - 循环中的正则表达式比 2.0 慢 200 倍(简单基准测试中的 3 倍)
- java - 更新的全局变量不会反映在 ValueEventListener 的 onCancelled 方法中
- angular - 类型“x”中缺少属性“jasmineMatches”
- java - 当没有字段是静态的时,为什么我的 arraylist 包含 n 个相同的元素?
- angular - 响应式材料设计
- android - 有时使用 googleapiclient 不会调用 OnLocationChanged
- python - 我可以在 python 的循环中调用 super 吗?
- python - 如何在python中合并两个不规则的数据框