子模块中的Android数据绑定

子模块中的Android数据绑定,第1张

概述我有一个应用程序模块,让我们说“测试”. “测试”模块取决于子模块B.两者都启用数据绑定.在库模块B中,我使用数据绑定创建一个简单的活动,其目的是为了可重用性,例如:我可以创建一个基本登录屏幕,并在以后的许多应用程序中使用它.以下是包B中的示例代码. package com.test.packageb open class MainActivity : AppCompatActivi 我有一个应用程序模块,让我们说“测试”. “测试”模块取决于子模块B.两者都启用数据绑定.在库模块B中,我使用数据绑定创建一个简单的活动,其目的是为了可重用性,例如:我可以创建一个基本登录屏幕,并在以后的许多应用程序中使用它.以下是包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类来做自定义事情,如下所示:

class MainActivity1 : MainActivity(){    overrIDe fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)    }    fun doSomething(){        binding.rootLayout.setBackgroundResource(R.color.colorPrimary)    }}

但是,当我尝试运行“测试”应用程序时,我收到此错误

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

我错过了什么?还有什么需要实施的吗?

测试app 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',module: 'support-annotations'    })    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"}
解决方法 不确定问题是否与您相关,但我设法找到某种解决方案.

为了使它工作基类应具有通用性

class A<BINDING extends VIEwDataBinding> {    protected ABinding binding;    voID init(){        binding = (ABinding) DataBindingUtil.setContentVIEw(this,R.layout.a);    }}

并将相同的绑定传递给子模块中的子类

class B<ABinding> {    // you can use instance in this class}

这里的主要问题是您无法彻底更改UI,只能隐藏现有元素或在运行时添加新元素.但我想在这种情况下更容易创建一个全新的类.

总结

以上是内存溢出为你收集整理的子模块中的Android数据绑定全部内容,希望文章能够帮你解决子模块中的Android数据绑定所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1124042.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存