Tag: android databinding

Android Studio 3.0数据绑定参考代码不由android studio生成

我遇到了Android Studio 3.0以及kotlin和数据绑定的问题。 我不知道为什么,但选项设置 – >编辑 – >数据绑定 – >由Android工作室生成的参考代码不工作,因为它应该是。 我已经尝试了不同的3.0版本(加那利3,加那利5,加那利6)与不同的kotlin(1.1.2-3,1.1.3,1.1.3-2)和数据绑定编译器版本。 为了确保我已经创建了一个干净的项目,并且出现了同样的问题。 我敢肯定,问题在于kotlin和数据绑定的结合,因为在同一个项目中,java类中的数据绑定工作得很好,数据绑定生成的文件每次更改.xml文件都会更新。 我不打算在这里粘贴我的每个gradle内容文件,因为我已经尝试了很多kotlin和数据绑定的组合版本。 下面是其中之一。 apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.dawidj.myapplication" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } […]

错误:未找到注释处理器'__gen.AnnotationProcessorWrapper_debug_android_databinding_annotationprocessor_ProcessDataBinding'

所以,我试图用Kotlin构建我的Android项目,并添加Android DataBinding库。 在Android Studio 2.2.3中没有问题。 但是在更新到最新的Android Studio版本(2.3.0)之后,当尝试运行应用程序时出现此错误。 这是我的项目的build.gradle buildscript { ext.kotlinVersion = '1.1.0' ext.androidGradlePluginVersion = '2.3.0' … dependencies { classpath "com.android.tools.build:gradle:${androidGradlePluginVersion}" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } } … 这里是我的应用程序的build.gradle apply plugin: 'com.android.application' apply plugin: 'com.neenbedankt.android-apt' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 25 buildToolsVersion "25.0.2" … packagingOptions { exclude 'META-INF/rxjava.properties' } sourceSets { […]

如何在Android Studio 3.0.0中使用数据绑定和Kotlin

我刚开始使用Android Studio 3.0.0,但每次尝试构建我的项目时都会出现此错误: Error:Circular dependency between the following tasks: :app:compileDebugKotlin +— :app:dataBindingExportBuildInfoDebug | \— :app:compileDebugKotlin (*) \— :app:kaptDebugKotlin \— :app:dataBindingExportBuildInfoDebug (*) (*) – details omitted (listed previously) 我在用 kapt "com.android.databinding:compiler:2.2.0" 在我使用之前 androidProcessor "com.android.databinding:compiler:2.2.0" 它工作得很好…我做错了什么? 谢谢!

Kotlin代表更有趣

如果您了解Google的实验性Android架构组件,您可能会知道MutableLiveData 。 试图使它更有趣使用我来与: class KotlinLiveData<T>(val default: T) { val data = MutableLiveData<T>() operator fun getValue(thisRef: Any?, property: KProperty<*>):T { return data.value ?: default } operator fun setValue(thisRef: Any?, property: KProperty<*>, value:T) { if (Looper.myLooper() == Looper.getMainLooper()) { data.value = value } else { data.postValue(value) } } } 然后我可以: var name : String by KotlinLiveData("not given") […]

任务之间的Gradle依赖关系

因此,自从添加了新的Room android架构库以来,这已经开始发生了。 我遇到了AppDatabase_Impl不存在的问题,我通过将kapt添加到注释中解决了这个问题: Android Room Persistences库和Kotlin Kotlin中的Room Persistence lib实现 Kotlin中的房间持久性库实现(Gradle错误) 我有其他的错误,我怀疑是由于AS,Kotlin和Java 8,所以我尝试更新到AS 3.0 我现在正在尝试构建时出现以下错误: Information:Gradle tasks [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:compileDebugSources, :app:compileDebugAndroidTestSources, :app:compileDebugUnitTestSources, :cryptocurrency-icons:generateDebugSources, :cryptocurrency-icons:mockableAndroidJar, :cryptocurrency-icons:generateDebugAndroidTestSources, :cryptocurrency-icons:compileDebugSources, :cryptocurrency-icons:compileDebugUnitTestSources, :cryptocurrency-icons:compileDebugAndroidTestSources] Error:Circular dependency between the following tasks: :app:compileDebugKotlin +— :app:dataBindingExportBuildInfoDebug | \— :app:compileDebugKotlin (*) \— :app:kaptDebugKotlin \— :app:dataBindingExportBuildInfoDebug (*) (*) – details omitted (listed previously) Information:BUILD FAILED in […]

DataBinding不能用在我在Kotlin编码的BaseActivity中

我想在我的Android项目中使用DataBinding,我在Kotlin中编写代码。 首先,我使用DataBindingUtil.setContentView(layoutResId)方法,它可以运行。 然后我尝试封装DataBinding到BaseActivity,gradle构建失败,我不知道为什么。 这是我的代码。 BaseActivity.kt class BaseActivity<B : ViewDataBinding> : AppCompatActivity(){ protected lateinit var mContext: AppCompatActivity protected lateinit var mBinding: B override fun onCreate(savedInstanceState: Bundle?) { // Dagger2 注入 AndroidInjection.inject(this) super.onCreate(savedInstanceState) // 保存当前 Context 对象 mContext = this // 添加到 AppManager 应用管理 AppManager.INSTANCE.addActivity(this) } override fun onDestroy() { // 从应用管理移除当前 Activity 对象 AppManager.INSTANCE.removeActivity(this) super.onDestroy() } […]

如何使用Android绑定库和Kotlin从视图绑定onClick事件?

我努力使Android绑定库与Kotlin一起工作。 我想要实现的是向我的Presenter类派发一个onClick事件。 我所做的是: 在模块的gradle文件上启用数据绑定:dataBinding dataBinding {enabled = true} 导入数据绑定编译器: kapt 'com.android.databinding:compiler:2.0.0-beta6' 生成存根: kapt {generateStubs = true} 在MainPresenter.kt上实现方法: fun onClickEditProfile () { log("method you hoped to get called was called") mView!!.getContext().snackbar("received event: onClickEditProfile via data binding, this is awesome").show() } 准备布局: <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable name="presenter" type="br.com.tyllt.presenter.MainPresenter" /> </data> <com.github.clans.fab.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="@{() -> presenter.onClickEditProfile()}" […]

在kotlin中平台类型的智能广播

我正在使用kotlin创建我的适配器,它在android中扩展了BaseAdapter 。 下面是getView方法内部的代码 override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? { var binding : ImageBinding var conView = convertView if(conView == null){ binding = DataBindingUtil.inflate(LayoutInflater.from(parent?.context), R.layout.image, parent, false) conView = binding.root; conView.tag = binding }else { binding = conView.getTag() as ImageBinding } return conView; } conView.tag = binding和binding = conView.getTag()突出显示粉红色的颜色。当我悬停在conView.tag = binding与我的鼠标弹出式消息智能转换到android.view.View! 。 […]

在使用Android Databinding和Kotlin的自定义BindingAdapter中使用lambda

我正在尝试使用Kotlin为Android Databinding绑定一个lambda处理程序。 只要我的ViewModel处理程序显式地返回Void就行了。 但是,如果它返回Kotlin单位,我得到一个error: cannot generate view binders java.lang.StackOverflowError 。 一切似乎都正确地钩上了所有其他自定义绑定工作(convertBooleanToViewVisibility,toTestString,甚至customOnClick只要我打电话onClickVoid [见下面的例子])。 问题是当我试图调用lambda返回单元在我的应用程序:customOnClick而不是返回Void。 在下面的例子中,它是调用mainViewModel.onClick()而不是mainViewModel.onClickVoid() 。 必须有一种方法可以使android:onClick能够调用Unit版本。 但是,如果我使用相同的语法调用onClick的customOnClick,我得到这个错误: :app:kaptGenerateStubsDebugKotlin Using kotlin incremental compilation :app:kaptDebugKotlin e: error: cannot generate view binders java.lang.StackOverflowError e: e: at android.databinding.tool.writer.Scope.access$getCurrentScope$cp(LayoutBinderWriter.kt:49) e: at android.databinding.tool.writer.Scope$Companion.getCurrentScope(LayoutBinderWriter.kt:58) e: at android.databinding.tool.writer.LayoutBinderWriterKt.scopedName(LayoutBinderWriter.kt:196) e: at android.databinding.tool.expr.Expr.toCode(Expr.java:776) e: at android.databinding.tool.writer.LayoutBinderWriterKt$callbackLocalName$2.invoke(LayoutBinderWriter.kt:203) e: at android.databinding.tool.writer.LayoutBinderWriterKt$callbackLocalName$2.invoke(LayoutBinderWriter.kt) e: at android.databinding.tool.ext.LazyExt.getValue(ext.kt:27) e: at android.databinding.tool.writer.LayoutBinderWriterKt.getCallbackLocalName(LayoutBinderWriter.kt) […]

Android数据绑定与Kotlin,BaseObservable和一个自定义委托

我正在尝试编写一个自定义的委托,它将清理Kotlin类中数据绑定的语法。 这将消除需要为每个我想要观察的属性定义一个自定义getter和setter。 Kotlin的标准实现如下所示: class Foo : BaseObservable() { var bar: String @Bindable get() = bar set(value) { bar = value notifyPropertyChanged(BR.bar) } } 显然,这个类有很多属性可以变得相当冗长。 我想要的是将其抽象成如下代表: class BaseObservableDelegate(val id: Int, private val observable: BaseObservable) { @Bindable operator fun getValue(thisRef: Any, property: KProperty<*>): Any { return thisRef } operator fun setValue(thisRef: Any, property: KProperty<*>, value: Any) { […]