mel*_*nke 5 android kotlin android-databinding
我试图在布局内使用伴随对象属性,但编译器无法识别它。
科特林班
class MyClass {
companion object {
val SomeProperty = "hey"
}
}
Run Code Online (Sandbox Code Playgroud)
XML布局
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fancy="http://schemas.android.com/tools">
<data>
<import type="package.MyClass"/>
</data>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@{MyClass.Companion.SomeProperty}"/>
</layout>
Run Code Online (Sandbox Code Playgroud)
我得到了这个错误:
e: java.lang.IllegalStateException: failed to analyze: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Could not find accessor package.MyClass.Companion.SomeProperty file:/path/to/my/layout.xml loc:21:67 - 21:103 ****\ data binding error ****
at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:138)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:154)
...
Caused by: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Could not find accessor package.MyClass.Companion.SomeProperty file:/path/to/my/layout.xml loc:21:67 - 21:103 ****\ data binding error ****
at android.databinding.tool.processing.Scope.assertNoError(Scope.java:112)
at android.databinding.annotationprocessor.ProcessDataBinding.doProcess(ProcessDataBinding.java:101)
at android.databinding.annotationprocessor.ProcessDataBinding.process(ProcessDataBinding.java:65)
...
Run Code Online (Sandbox Code Playgroud)
我尝试使用companion代替Companion,但是没有运气。
是否可以在具有数据绑定的xml布局上使用伴随对象?我该如何进行?在此先感谢您的帮助:)
为了访问Companion对象的属性和方法,它不要求有父对象的实例。伴侣对象已实例化,因此您可以直接访问该实例。
代替使用<import>(这是Java的自然翻译),我们需要使用<variable>,因为我们实际上想在XML布局中使用(已实例化的)Companion对象。
如下导入您的伴侣对象
鉴于科特林课程:
package com.example.project
class MyViewModel {
companion object {
// it is only working with val and var
// const val wouldn't work
val MAX_LENGTH = 10
}
}
Run Code Online (Sandbox Code Playgroud)
布局:
<data>
<!-- Declare your "variable" that hold the Companion object itself -->
<variable name="myViewModelStatic" type="com.example.project.MyViewModel.Companion" />
</data>
<!-- then use the myViewModelStatic to access "static" properties of MyViewModel -->
<EditText
...
android:maxLength="@{ myViewModelStatic.MAX_LENGTH }"
/>
</layout>
Run Code Online (Sandbox Code Playgroud)
分段:
class MyFragment {
...
onViewCreated(...) {
// now bind the companion object to the variable declared in the XML
binding.myViewModelStatic = TransferUseCase.Companion
}
...
}
Run Code Online (Sandbox Code Playgroud)
在 XML 中,只需在字段名称前添加Companion ,例如:
在视图模型中
package com.example.project
class MyViewModel {
companion object {
var leText = "text"
}
var leColor = ...
}
Run Code Online (Sandbox Code Playgroud)
在 XML 中
<data>
<import type="android.view.View" />
<variable
name="context"
type="com.example.project.MyViewModel" />
</data>
<TextView
...
android:text="@{context.Companion.leText}"
android:color="@{context.leColor}"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1675 次 |
| 最近记录: |