java - Android Studio 3.6.3 不生成所有 BE 常量

标签 java android android-studio data-binding

我一直在使用绑定(bind)(在操作和 fragment 中),直到我设法让它(部分)工作。我绑定(bind)了六个变量(它们在应用程序中正确显示),但生成的 BR 文件只有三个定义(其中只有一个对应于我的变量之一)

我的主要布局

<?xml version="1.0" encoding="utf-8"?>

<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.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" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

内容主要:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>

fragment 的布局:

<?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="data"
            type="Data" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".FragmentFloor0">

        <TextView
            android:id="@+id/label_floor0"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_small"
            android:layout_marginTop="@dimen/margin_small"
            android:layout_marginEnd="@dimen/margin_small"
            android:text="@string/label_floor0"
            android:textSize="@dimen/textSizeRegular"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/imageGroundFloor"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="@dimen/margin_small"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/label_floor0"
            app:layout_constraintVertical_bias="0.0"
            app:srcCompat="@drawable/ground_floor_v2"
            android:scaleType="centerInside"
            android:contentDescription="@string/blueprint_0th_floor" />

        <TextView
            android:id="@+id/liviTempStr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{data.fl0_liviTempStr}"
            android:textSize="@dimen/textSizeRegular"
            app:layout_constraintBottom_toBottomOf="@id/imageGroundFloor"
            app:layout_constraintEnd_toEndOf="@id/imageGroundFloor"

            app:layout_constraintHorizontal_bias="0.788"
            app:layout_constraintStart_toStartOf="@id/imageGroundFloor"
            app:layout_constraintTop_toTopOf="@id/imageGroundFloor"
            app:layout_constraintVertical_bias="0.32" />

        <TextView
            android:id="@+id/liviTempTel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{data.fl0_liviTempTel}"
            android:textSize="@dimen/textSizeRegular"
            app:layout_constraintBottom_toBottomOf="@id/imageGroundFloor"
            app:layout_constraintEnd_toEndOf="@id/imageGroundFloor"

            app:layout_constraintHorizontal_bias="0.95"
            app:layout_constraintStart_toStartOf="@id/imageGroundFloor"
            app:layout_constraintTop_toTopOf="@id/imageGroundFloor"
            app:layout_constraintVertical_bias="0.65" />

        <TextView
            android:id="@+id/kitcTempSin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{data.fl0_kitcTempSin}"
            android:textSize="@dimen/textSizeRegular"
            app:layout_constraintBottom_toBottomOf="@id/imageGroundFloor"
            app:layout_constraintEnd_toEndOf="@id/imageGroundFloor"

            app:layout_constraintHorizontal_bias="0.32"
            app:layout_constraintStart_toStartOf="@id/imageGroundFloor"
            app:layout_constraintTop_toTopOf="@id/imageGroundFloor"
            app:layout_constraintVertical_bias="0.65" />

        <TextView
            android:id="@+id/bathTempSin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{data.fl0_bathTempSin}"
            android:textSize="@dimen/textSizeRegular"
            app:layout_constraintBottom_toBottomOf="@id/imageGroundFloor"
            app:layout_constraintEnd_toEndOf="@id/imageGroundFloor"

            app:layout_constraintHorizontal_bias="0.95"
            app:layout_constraintStart_toStartOf="@id/imageGroundFloor"
            app:layout_constraintTop_toTopOf="@id/imageGroundFloor"
            app:layout_constraintVertical_bias="0.8" />

        <TextView
            android:id="@+id/hallTempSin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{data.fl0_hallTempEx1}"
            android:textSize="@dimen/textSizeRegular"
            app:layout_constraintBottom_toBottomOf="@id/imageGroundFloor"
            app:layout_constraintEnd_toEndOf="@id/imageGroundFloor"

            app:layout_constraintHorizontal_bias="0.62"
            app:layout_constraintStart_toStartOf="@id/imageGroundFloor"
            app:layout_constraintTop_toTopOf="@id/imageGroundFloor"
            app:layout_constraintVertical_bias="0.39" />

        <TextView
            android:id="@+id/hallTempEx1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{data.fl0_hallTempEx1}"
            android:textSize="@dimen/textSizeRegular"
            app:layout_constraintBottom_toBottomOf="@id/imageGroundFloor"
            app:layout_constraintEnd_toEndOf="@id/imageGroundFloor"
            android:rotation="90"
            app:layout_constraintHorizontal_bias="0.65"
            app:layout_constraintStart_toStartOf="@id/imageGroundFloor"
            app:layout_constraintTop_toTopOf="@id/imageGroundFloor"
            app:layout_constraintVertical_bias="0.55" />

        <TextView
            android:id="@+id/hallHummEx1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{data.fl0_hallHummEx1}"
            android:rotation="90"
            android:textSize="@dimen/textSizeRegular"
            app:layout_constraintBottom_toBottomOf="@id/imageGroundFloor"
            app:layout_constraintEnd_toEndOf="@id/imageGroundFloor"

            app:layout_constraintHorizontal_bias="0.65"
            app:layout_constraintStart_toStartOf="@id/imageGroundFloor"
            app:layout_constraintTop_toTopOf="@id/imageGroundFloor"
            app:layout_constraintVertical_bias="0.65" />

        <Button
            android:id="@+id/button_floor0_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/margin_small"
            android:layout_marginBottom="@dimen/margin_small"
            android:text="@string/button_next"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

数据绑定(bind)类:

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;

public class HomematicData extends BaseObservable  {
    private float fl0_liviTempStr;
    private float fl0_liviTempTel;
    private float fl0_kitcTempSin;
    private float fl0_bathTempSin;
    private float fl0_hallTempEx1;
    private float fl0_hallHummEx1;

    @Bindable
    public float getFl0_liviTempStr() { return fl0_liviTempStr; }
    public float getFl0_liviTempTel() { return fl0_liviTempTel; }
    public float getFl0_kitcTempSin() { return fl0_kitcTempSin; }
    public float getFl0_bathTempSin() { return fl0_bathTempSin; }
    public float getFl0_hallTempEx1() { return fl0_hallTempEx1; }
    public float getFl0_hallHummEx1() { return fl0_hallHummEx1; }
    public void setFl0_liviTempTel(float fl0_liviTempTel) { this.fl0_liviTempTel = fl0_liviTempTel; /* notifyPropertyChanged(BR.fl0_liviTempTel); */ }
    public void setFl0_liviTempStr(float fl0_liviTempStr) { this.fl0_liviTempStr = fl0_liviTempStr; /* notifyPropertyChanged(BR.fl0_liviTempStr); */ }
    public void setFl0_kitcTempSin(float fl0_kitcTempSin) { this.fl0_kitcTempSin = fl0_kitcTempSin; /* notifyPropertyChanged(BR.fl0_kitcTempSin); */ }
    public void setFl0_bathTempSin(float fl0_bathTempSin) { this.fl0_bathTempSin = fl0_bathTempSin; /* notifyPropertyChanged(BR.fl0_ballTempSin); */ }
    public void setFl0_hallTempEx1(float fl0_hallTempEx1) { this.fl0_hallTempEx1 = fl0_hallTempEx1; /* notifyPropertyChanged(BR.fl0_hallTempEx1); */ }
    public void setFl0_hallHummEx1(float fl0_hallHummEx1) { this.fl0_hallHummEx1 = fl0_hallHummEx1; /* notifyPropertyChanged(BR.fl0_hallTempEx1); */ }

主要 Activity :

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>

fragment Activity :

public class FragmentFloor0 extends Fragment {
    private FragmentFloor0Binding binding;

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState ) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_floor0, container, false);

        View rootView = binding.getRoot();
        HomematicData hd = new HomematicData();

        hd.setFl0_liviTempStr(1.0f);
        hd.setFl0_liviTempTel(2.0f);
        hd.setFl0_bathTempSin(3.0f);
        hd.setFl0_kitcTempSin(4.0f);
        hd.setFl0_hallTempEx1(5.0f);
        hd.setFl0_hallHummEx1(6.0f);
        binding.setData(hd);

        return rootView;
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.findViewById(R.id.button_floor0_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavHostFragment.findNavController(FragmentFloor0.this)
                        .navigate(R.id.action_FragmentFloor0_to_FragmentFloor1);
            }
        });
    }
}

绑定(bind)适配器:

public class BindingUtils {

    @BindingAdapter("android:text")
    public static void setFloat(TextView view, float value) {
        if (Float.isNaN(value)) view.setText("");
        else view.setText(String.format("%.1f", value));
    }

    @InverseBindingAdapter(attribute = "android:text")
    public static float getFloat(TextView view) {
        String num = view.getText().toString();
        if(num.isEmpty()) return 0.0F;
        try {
            return Float.parseFloat(num);
        } catch (NumberFormatException e) {
            return 0.0F;
        }
    }

并生成 BR:

package androidx.databinding.library.baseAdapters;

public class BR {
  public static final int _all = 0;

  public static final int data = 1;

  public static final int fl0_liviTempStr = 2;
}

所有其他常量在哪里?!

我有干净构建和重建项目,以及使缓存无效并重建项目。

最佳答案

在我看来,@Bindable 需要在每个 getter 之前添加

关于java - Android Studio 3.6.3 不生成所有 BE 常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61441724/

相关文章:

android - Android 上的透明 LinearLayout

android - 区域设置更改后防止应用程序语言更改

android - 尝试实现 ar core 时无法加载 Renderable

android - Android Studio Gradle插件

java - 如何在 ScrollView 中将广告固定到屏幕上?

java - 有什么方法可以按月查找推文的数量吗?

java - 从命令 com.google.analyticsreporting.v4 包运行的 Google Analytics API 4 Java 客户端不存在

java - 找不到 Artifact org.apache.maven.plugins :maven-resources-plugin

android - AutoCompleteTextView - 当我们在过滤后有一个完全匹配项时如何禁用下拉列表?

java - 在 Android Studio 中,方法注释未显示在智能感知中