子模块中的Android数据绑定 [英] Android Databinding in Sub-module
问题描述
我有一个应用程序模块,比方说测试". 测试"模块取决于子模块B.两者均启用数据绑定.在库模块B中,我使用数据绑定创建了一个简单的活动,其目的是为了可重用性,例如:我可以创建一个基本的Login屏幕,以后在许多应用程序中使用它.下面是程序包B中的示例代码.
I have an application module, let's say "Test". The "Test" module depends on a sub-module B. Both enable databinding. In library module B, I create a simple activity using databinding, its purpose is for reusability, for example: I can create a base Login screen and use it in many apps later. Below is sample code in Package B.
package com.test.packageb
open class MainActivity : AppCompatActivity() {
lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
}
}
然后在测试"模块中,我可以简单地继承MainActivity类来进行自定义,如下所示:
and then in "Test" module, I can just simply inherit MainActivity class to do customize things, like this:
class MainActivity1 : MainActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
fun doSomething(){
binding.rootLayout.setBackgroundResource(R.color.colorPrimary)
}
}
但是,当我尝试运行测试"应用程序时,出现此错误
However, when I try to run "Test" application, I got this error
错误:(17,9)无法访问类 'com.test.packageb.databinding.ActivityMainBinding'.检查你的模块 缺少或冲突的依赖项的类路径
错误:(17、17) 未解决的参考:rootLayout
Error:(17, 9) Cannot access class 'com.test.packageb.databinding.ActivityMainBinding'. Check your module classpath for missing or conflicting dependencies
Error:(17, 17) Unresolved reference: rootLayout
我想念什么?还有什么需要实施的吗?
What did I miss? Is there anything else need to be implemented?
测试应用的build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.test.testapplication"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
dataBinding{
enabled true
}
buildTypes {
debug {
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
kapt 'com.android.databinding:compiler:3.0.0-beta4'
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation project(':packageb')
}
B包build.gradle
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
dataBinding{
enabled true
}
buildTypes {
debug {
minifyEnabled false
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
kapt 'com.android.databinding:compiler:3.0.0-beta4'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
推荐答案
不确定该问题是否与您有关,但我设法找到了某种解决方案.
Not sure if the issue is relevant to you but I managed to find some kind of the solution.
要使其正常工作,基类应具有泛型
To make it work base class should have generic
class A<BINDING extends ViewDataBinding> {
protected ABinding binding;
void init(){
binding = (ABinding) DataBindingUtil.setContentView(this, R.layout.a);
}
}
并将相同的绑定从子模块
and pass the same binding to the child class from the submodule
class B<ABinding> {
// you can use instance in this class
}
这里的主要问题是您无法彻底更改UI,只能隐藏现有元素或在运行时添加新元素.但我想在这种情况下,创建一个全新的类会更容易.
The main problem here that you can't drastically change the UI, only hiding existing elements or add new at runtime. But I suppose in such case easier create a totally new class.
这篇关于子模块中的Android数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!