Android'de özel bir bindingAdapter yarattım ve rengini değiştirdiğimde renginin değişmesini istiyorum, aslında bu bir test için çalışıyor. . İşte kod: burada bağlama verileri için yaptığım görünüm Modeli geçerli:Android dataBinding - @BindingAdapter özel uygulama ad alanı göz ardı ediliyor
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"));
}
} beklenen bu model im binded benim xml dosyasında Şimdi
bir renkte geçmesine böylece SetTextColor yöntem kullanabilirsiniz:
<?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>
ben çalışma zamanı sırasında hata alıyorum:
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.
bindingAdapter öğelerini çıkarırsam, diğer veri bağlama araçlarıyla mükemmel çalışır. Sadece bu özel ciltleme çalışmıyor. Projemde floatingactionbuttonbasic btw başlıklı.
Ayrıca uyarıyı da çözdünüz mü? 'Uyarı: Nitelik uygulaması için uygulama ad alanı: bindColor göz ardı edilecektir. ' – Calin
Oh, nevermind, bu adam bunu çözdü https://stackoverflow.com/questions/35313466/android-databinding-custom-binding-adapter-warning – Calin