Stworzyłem niestandardowy bindingAdapter w Androidzie i kiedy przechodzę w kolorze, który chcę zmienić kolor, to jest tak naprawdę dla testu, nad którym pracuję, aby upewnić się, że działa . Oto kod: tu jest mój widok modelu dla wiązania danych:Android dataBinding - @BindingAdapter Przestrzeń nazw aplikacji niestandardowych jest ignorowana
public class User {
public ObservableInt visible;
public User(int visible) {
this.visible=new ObservableInt(visible);
}
@BindingAdapter({"app:bindColor"}) //notice the bindColor custom attribute
public static void setTextColor(TextView view,String color) {
if("green".equals(color))
view.setTextColor(Color.parseColor("#63f421"));
}
}
Teraz w moim pliku XML, który jest binded do tego modelu im oczekiwanego przejść w kolorze tak, że SetTextColor metoda może go używać:
<?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">
<data class="MainActivityBinder">
<variable name="user" type="com.example.android.floatingactionbuttonbasic.User"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_one"
android:text="my first textview"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_two"
android:text="my second textview"
android:visibility="@{user.visible}"
app:bindColor="@{'green'}" //see im passing in the green string here
android:textColor="@android:color/holo_green_dark"/>
</LinearLayout>
</layout>
otrzymuję błąd podczas środowiska wykonawczego:
Error:(27, 65) error: package com.example.android.floatingactionbuttonbasic.databinding does not exist
Warning:Application namespace for attribute app:bindColor will be ignored.
Error:(24, 33) Identifiers must have user defined types from the XML file. een is missing it
Error:Execution failed for task ':Application:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
, jeśli wyjmę elementy bindingAdapter, które działają idealnie z innymi elementami związanymi z wiązaniem danych. To tylko niestandardowe wiązanie, które nie działa. Mój projekt nosi tytuł floatingactionbuttonbasic btw.
Czy kiedykolwiek rozwiązałeś również ostrzeżenie? Ostrzeżenie: Obszar nazw aplikacji dla aplikacji atrybutów: bindColor zostanie zignorowany. – Calin
Och, nieważne, ten facet go rozwiązał https://stackoverflow.com/questions/35313466/android-databinding-custom- binding-adapter-warning – Calin