问题
Android Studio 3.6. Canary 12
build.gradle:
buildscript {
ext.kotlin_version = '1.3.50'
ext.RETROFIT_VERSION = '2.6.0'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.0-alpha12'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
in app/build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"
android {
viewBinding {
enabled = true
}
dataBinding {
enabled = true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
compileSdkVersion 29
defaultConfig {
vectorDrawables.useSupportLibrary = true
applicationId "com.android.testproject.android.kotlin"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
in layout xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="android.view.View" />
<variable
name="handler"
type="com.android.testproject.android.kotlin.coroutine_retrofit.ui.activity.CoroutinesRetrofitActivity" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolBarContainer"
layout="@layout/tool_bar"
android:title='@{@string/coroutine_retrofit}'
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="@+id/buttonRetry"
android:layout_width="wrap_content"
android:layout_height="@dimen/min_height"
android:visibility="gone"
android:text="@string/retry"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/agentsRecyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="@{handler.agentList.size > 0 ? View.VISIBLE : View.GONE}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolBarContainer"
tools:listitem="@layout/agent_list_item" />
<TextView
android:id="@+id/noActivityTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/no_agents"
android:visibility="@{handler.isVisibleNoItems ? View.VISIBLE : View.GONE}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />
<include
layout="@layout/progress_bar_layout"
android:visibility="gone" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
here progress_bar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/containerProgressBarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4777"
android:clickable="true"
android:elevation="2dp"
android:focusable="true">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="@dimen/min_height"
android:layout_height="@dimen/min_height"
android:indeterminateTint="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here CoroutinesRetrofitActivity.kt
class CoroutinesRetrofitActivity : AppCompatActivity(), AgentListItemAdapter.AdapterListener {
var agentList = ObservableArrayList<Agent>()
private lateinit var binding: CoroutinesRetrofitActivityBinding
private lateinit var coroutinesRetrofitViewModel: CoroutinesRetrofitViewModel
val isVisibleNoItems = ObservableBoolean()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = CoroutinesRetrofitActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.setHandler(this);
this.coroutinesRetrofitViewModel.getIsShowProgress()
.observe(this, object : Observer<Boolean> {
override fun onChanged(isShowProgress: Boolean?) {
findViewById<View>(R.id.containerProgressBarLayout).visibility =
if (isShowProgress!!) View.VISIBLE else View.GONE
}
});
}
But when I try to build I get error:
> Task :app:checkDebugDuplicateClasses
> Task :app:javaPreCompileDebug
> Task :app:compileDebugJavaWithJavac
\testProjects\android\TestProjectAndroidKotlin\app\build\generated\source\kapt\debug\com\android\testproject\android\kotlin\databinding\CoroutinesRetrofitActivityBindingImpl.java:48: error: incompatible types: ProgressBarLayoutBinding cannot be converted to ViewDataBinding
setContainedBinding(this.mboundView01);
But if I remove from xml this:
<include
layout="@layout/progress_bar_layout"
android:visibility="gone" />
then error is gone.
What is wrong with progress_bar_layout
?
If I direct include progress bar in layout like this:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="android.view.View" />
<variable
name="handler"
type="com.android.testproject.android.kotlin.coroutine_retrofit.ui.activity.CoroutinesRetrofitActivity" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolBarContainer"
layout="@layout/tool_bar"
android:title='@{@string/coroutine_retrofit}'
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="@+id/buttonRetry"
android:layout_width="wrap_content"
android:layout_height="@dimen/min_height"
android:text="@string/retry"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/agentsRecyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="@{handler.agentList.size > 0 ? View.VISIBLE : View.GONE}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolBarContainer"
tools:listitem="@layout/agent_list_item" />
<TextView
android:id="@+id/noActivityTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/no_agents"
android:visibility="@{handler.isVisibleNoItems ? View.VISIBLE : View.GONE}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />
<ProgressBar
android:id="@+id/containerProgressBarLayout"
style="?android:attr/progressBarStyle"
android:layout_width="@dimen/min_height"
android:layout_height="@dimen/min_height"
android:indeterminateTint="@color/colorPrimary"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
the problem is gone.
Why with include
not work?
include
layout="@layout/progress_bar_layout"
android:visibility="gone" />
回答1:
I found solution.
If outer xml is data binding xml, then inner xml also must be data binding.
So progress_bar_layout.xml
must be like this:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/containerProgressBarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#66545a5a"
android:clickable="true"
android:elevation="2dp"
android:focusable="true">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="@dimen/min_height"
android:layout_height="@dimen/min_height"
android:indeterminateTint="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
回答2:
In progress_bar_layout use below line (m also not sure it works for you or not because I didn't try yet but if it works then I will add more clarification)
Maybe this error is for combining view binding and data binding. View binding doesn't support layout variables
<androidx.constraintlayout.widget.ConstraintLayout
...
tools:viewBindingIgnore="true" >
...
</androidx.constraintlayout.widget.ConstraintLayout>
来源:https://stackoverflow.com/questions/58147214/view-binding-not-work-with-include-incompatible-types-progressbarlayoutbindi