Я играл с привязками (в действиях и фрагментах), пока мне не удалось заставить его (частично) работать. Я связываю шесть переменных (и они правильно отображаются в приложении), но сгенерированный файл 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>
Макет фрагмента:
<?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>
Класс привязки данных:
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); */ }
Основная деятельность:
<?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>
Действие фрагмента:
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);
}
});
}
}
Адаптер связывания:
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;
}
где находятся все остальные константы? !
У меня есть проект чистой сборки и перестройки, а также недействительный кеш и перестройка проекта.