我在我的应用程序中使用数据绑定(bind)。我有一个应用程序模块,数据绑定(bind)成功运行。

但是,我也有一个“home-module”,我试图使用相同的技术,但是xml中的数据绑定(bind)出现以下错误:



我发现this bug report表示这是一个问题,但已修复。

我看不到代码有任何问题,我认为问题是因为它在库模块中。

我能做些什么才能使它正常工作?

主模块

activity_home.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>
        <variable name="viewModel" type="com.flowmellow.projectx.home.ui.viewmodel.HomeViewModel"/>
    </data>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/home_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.flowmellow.projectx.home.ui.viewmodel.HomeActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

        </android.support.design.widget.AppBarLayout>

        <android.support.constraint.ConstraintLayout
            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"
            android:id="@+id/content_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context="com.flowmellow.projectx.home.ui.viewmodel.HomeActivity">

            <TextView
                android:text="@string/activity_home_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/activity_home_title"
                android:textAppearance="@style/TextAppearance.AppCompat.Large"
                android:layout_marginStart="16dp"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_marginLeft="16dp"
                android:layout_marginEnd="16dp"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginRight="16dp"
                app:layout_constraintBottom_toTopOf="@+id/activity_home_button"
                android:layout_marginBottom="64dp" />

            <Button
                android:text="@string/activity_home_button"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/activity_home_button"
                style="@style/Widget.AppCompat.Button.Colored"
                android:onClick="@{viewModel::onButtonClicked}"/>

        </android.support.constraint.ConstraintLayout>

    </android.support.design.widget.CoordinatorLayout>
</layout>

家庭 Activity
public class HomeActivity extends AppCompatActivity {

    private HomeViewModel mHomeViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        final CustomSensorManager customSensorManager = new CustomSensorManager(sensorManager);
        mHomeViewModel = new HomeViewModel(customSensorManager);

        setBindingContentView(R.layout.activity_home);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    private void setBindingContentView(@LayoutRes int layoutResID) {
        ActivityHomeBinding binding = DataBindingUtil.setContentView(this, layoutResID);
        binding.setViewModel(mHomeViewModel);
    }
}

HomeViewModel
public class HomeViewModel {

    private CustomSensorManager mSensorManager;

    public HomeViewModel(@NonNull CustomSensorManager sensorManager) {
        mSensorManager = sensorManager;
    }

    public void onButtonClicked(View view) {
        mSensorManager.scan(true);
    }
}

build.gradle
apply plugin: 'com.android.library'
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')

    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.android.support:design:25.0.0'
    compile 'com.android.support.constraint:constraint-layout:+'
}

更新:

可能是一个错误。 link

我的问题:

android - DataBinding在模块中不起作用-LMLPHP

引擎模块没有UI,因此错过了依赖关系,现在很有意义了。

最佳答案

假设您的应用中具有这样的多级模块层次结构:

android - DataBinding在模块中不起作用-LMLPHP

为了将数据绑定(bind)到工作模块A,必须确保在中启用数据绑定(bind),将整个层次结构从根模块导入到模块A中。

这意味着您必须添加:

dataBinding {
    enabled = true
}

到模块:
  • 应用程序模块
  • 模块
  • 模块B

  • 重要的是要记住,您在中添加了来也启用了module B中的数据绑定(bind),因此涵盖了所有路径。

    关于android - DataBinding在模块中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41765114/

    10-11 22:25
    查看更多