app:visibleGone cannot resolve on android Databind

2020-08-17 05:32发布

问题:

When I build the project. I'm getting error on app:visibleGone

I'm also enable true to dataBinding in build.gradle and using android architecture components and mvvm. project targetSdkVersion is 26 and support lib version is 26.0.1.

Below is the error message

error: package com.****.****.databinding does not exist
error: cannot find symbol class ActivityMainBinding
Cannot find the setter for attribute 'app:visibleGone' with parameter type boolean on android.widget.Button.

here is my activity_main.xml

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

    <data>

        <variable
            name="loading"
            type="boolean" />

    </data>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.aungmyolwin.importdb.MainActivity">

        <Button
            android:id="@+id/btn_load_sql"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Load from SQL"
            app:visibleGone="@{!loading}"/>

        <Button
            android:id="@+id/btn_load_room"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Load from Room mapper"
            app:visibleGone="@{!loading}"/>

        <TextView
            android:id="@+id/tv_import_loading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Importing database...."
            app:visibleGone="@{loading}"/>


    </LinearLayout>
</layout>

ActivityMain.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        binding.btnLoadRoom.setOnClickListener(this);
        binding.btnLoadSql.setOnClickListener(this);

        viewModels= ViewModelProviders.of(this).get(MainActivityViewModels.class);

    }
}

回答1:

You need to create a custom BindingAdapter for app:visibleGone (because it is not a available method).
Like

public class BindingAdapters {
    @BindingAdapter("visibleGone")
    public static void showHide(View view, boolean show) {
        view.setVisibility(show ? View.VISIBLE : View.GONE);
    }
}

Moreover, if you don't want to define a method like this, you can do like

<layout xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="loading"
            type="boolean" />
        <import type="android.view.View"/> <!-- remember to import -->
    </data>

    <LinearLayout >
        <Button
            android:visibility="@{loading ? View.GONE : View.VISIBLE}"
    </LinearLayout>
</layout>