26

安卓工作室 3.6。金丝雀 12

构建.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
    }
}

在 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"
    }

在布局 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>

这里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>

这里是 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
                }
            });

    }

但是当我尝试构建时出现错误:

> 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);

但是如果我从 xml 中删除这个:

 <include
            layout="@layout/progress_bar_layout"
            android:visibility="gone" />

然后错误消失了。

有什么问题progress_bar_layout

如果我直接在布局中包含进度条,如下所示:

<?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>

问题消失了。

为什么include不工作?

include
            layout="@layout/progress_bar_layout"
            android:visibility="gone" />
4

4 回答 4

46

我找到了解决方案。如果外部 xml 是数据绑定 xml,那么内部 xml 也必须是数据绑定。所以progress_bar_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">

    <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>
于 2019-11-23T15:26:03.777 回答
9

在 progress_bar_layout 使用下面的行(我也不确定它是否适合你,因为我还没有尝试,但如果它有效,那么我会添加更多说明)

也许这个错误是为了结合视图绑定和数据绑定。视图绑定不支持布局变量

<androidx.constraintlayout.widget.ConstraintLayout
    ...
    tools:viewBindingIgnore="true" >
...
</androidx.constraintlayout.widget.ConstraintLayout>
于 2019-09-28T15:50:41.597 回答
3

除了现有的答案。如果您的布局使用布局(数据绑定)标签作为父级,那么您必须将包含的布局包装在布局(数据绑定)标签内。下面是源代码。

包括的布局

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <matrixsystems.core.widgets.CustomToolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/app_white"
        android:fitsSystemWindows="true"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

</layout>

布局

<include
        android:id="@+id/toolbar"
        layout="@layout/toolbar_app" />

活动

override fun onCreate(savedInstanceState: Bundle?) {
    // getting toolbar from binding
    setSupportActionBar(binding.toolbar.root as Toolbar)
}
于 2020-10-04T07:24:40.997 回答
3

我使用了 a_subsriber答案,它确实有效,但请务必将 xmlns 命名空间放在布局标签中,但不要放在视图标签中

<layout xmlns:android="http://schemas.android.com/apk/res/android">
于 2020-04-01T13:07:38.667 回答