数据绑定:ButterKnife,Kotlin:不生成绑定文件
Android Studio 3.0
在项目/ build.gradle:
classpath 'com.android.tools.build:gradle:3.0.1'
在应用程序/ build.gradle:
buildscript { repositories { maven { url 'https://maven.fabric.io/public' } } dependencies { classpath 'io.fabric.tools:gradle:1.+' } } apply plugin: 'com.android.application' apply plugin: 'io.fabric' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' apply plugin: 'realm-android' def keystoreProperties = new Properties() keystoreProperties.load(new FileInputStream(rootProject.file("app/keystore.properties"))) android { dataBinding { enabled = true } compileSdkVersion 26 defaultConfig { applicationId "com.myproject.android.customer" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "0.1.1" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" //multiDexEnabled true } signingConfigs { release { keyAlias keystoreProperties['KEY_ALIAS_RELEASE'] keyPassword keystoreProperties['KEY_PASSWORD_RELEASE'] storeFile file(keystoreProperties['STORE_FILE_RELEASE']) storePassword keystoreProperties['STORE_PASSWORD_RELEASE'] } } /*- exclude buildTypes = "debug" from build Variants variantFilter { variant -> if (variant.buildType.name.equals('debug')) { variant.setIgnore(true); } }*/ buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' // sign settings signingConfig signingConfigs.release // config Fabrice (Beta of Crashlytics) ext.betaDistributionReleaseNotes = defaultConfig.versionName + " " + name ext.betaDistributionEmailsFilePath = "app/beta_distribution_emails.txt" } debug { ext.alwaysUpdateBuildId = false // not need CrashLytics on debug mode } } }
依赖关系{implementation fileTree(dir:’libs’,include:[‘* .jar’])
implementation('com.crashlytics.sdk.android:crashlytics:2.7.0@aar') { transitive = true; } implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support:design:26.1.0' implementation 'com.android.support:recyclerview-v7:26.1.0' // must use pdf-viewer:1.6.0 implementation 'com.github.barteksc:android-pdf-viewer:1.6.0' //implementation 'com.github.markomilos:paginate:0.5.1' implementation 'com.google.android.gms:play-services-location:11.4.2' implementation 'com.google.android.gms:play-services-maps:11.4.2' implementation "com.github.bumptech.glide:glide:$GLIDE_VERSION" implementation "com.jakewharton:butterknife:$BUTTER_KNIFE_VERSION" implementation 'com.jayway.jsonpath:json-path:2.4.0' implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0' implementation 'com.squareup.retrofit2:converter-gson:2.1.0' implementation 'com.squareup.retrofit2:retrofit:2.3.0' implementation 'commons-io:commons-io:2.5' implementation 'io.realm:android-adapters:2.0.0' implementation 'org.greenrobot:eventbus:3.0.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$KOTLIN_VERSION" implementation project(':common') kapt "com.github.bumptech.glide:compiler:$GLIDE_VERSION" kapt "com.jakewharton:butterknife-compiler:$BUTTER_KNIFE_VERSION" // test dependencies testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
repositories { mavenCentral() maven { url 'https://maven.fabric.io/public' } }
我的布局名称是: offer_item.xml
。
所以作为结果绑定文件名是: OfferItemBinding
在这里我的RecyclerViewAdapter:
import com.myproject.android.customer.databinding.OfferItemBinding; public OfferSortAdapter.OfferViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); OfferItemBinding binding = OfferItemBinding.inflate(inflater, parent, false); return new OfferViewHolder(binding.getRoot(), this); }
这里xml布局:
文件OfferItemBinding
没有生成。
我得到错误:
D:\dev\MyProject\app\src\main\java\com\myproject\android\customer\ui\adapter\OfferSortAdapter.java:29: error: package com.myproject.android.customer.databinding does not exist import com.myproject.android.customer.databinding.OfferItemBinding;
尝试在build.gradle
dependencies { //for butterknife implementation 'com.jakewharton:butterknife:8.8.1' kapt 'com.jakewharton:butterknife-compiler:8.8.1' implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" kapt "com.android.databinding:compiler:2.3.3" } apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android' kapt { generateStubs = true }
在你的RecylerviewAdpater中
public class MyViewHolder extends RecyclerView.ViewHolder { private final ItemBinding binding; public MyViewHolder(ItemBinding binding) { super(binding.getRoot()); this.binding = binding; } public void bind(Item item) { binding.setItem(item); binding.executePendingBindings(); } }
现在,我的适配器可以使用Android数据绑定来创建和绑定:
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); ItemBinding itemBinding = ItemBinding.inflate(layoutInflater, parent, false); return new MyViewHolder(itemBinding); } public void onBindViewHolder(MyViewHolder holder, int position) { Item item = getItemForPosition(position); holder.bind(item); }
- 使用房间数据库(房间持久性库)创建触发器
- kotlin android – 用Builder模式和Java 8 lambda定制对话框
- 将屏幕坐标转换为OpenGL坐标
- 测试环境配置:Android + JUnit 5 + Mockito + Spek + Kotlin
- 当我添加将RealmObject扩展到我的项目的ChatInformation kotlin类时,无法建立项目,错误在哪里?
- 任务之间的Gradle依赖关系
- kotlin库模块中调试目录的未解决的参考
- 是否有可能通过intent-filter和broadcastreciever获取剪贴板管理器?
- 如何实现一个延迟加载recyclerView适配器,像使用kotlin的realm recyclerView适配器