我正在编写具有2路数据绑定的应用程序。我可以获取编辑文本字段进行更新,但是我也正在尝试更新内部逻辑。我尝试通过在绑定上调用addOnPropertyChangedCallback(Observable.OnPropertyChangedCallback)
来做到这一点,但是尽管检查了notifyPropertyChanged(int)
是否由可观察对象调用,但并未调用回调
我尝试将@Bindable
批注添加到in this stackoverflow question的getter和setter上,但无济于事。
class AddItemObservable : BaseObservable(), AddItemImmutableObservable {
private var name = ""
private var quantity: Int? = null
@Bindable
override fun getName() = name
fun setName(name: String) {
if (this.name == name) {
return
}
this.name = name
notifyPropertyChanged(BR.name)
}
@Bindable
override fun getQuantity() = quantity
fun setQuantity(quantity: Int?) {
if (this.quantity?.equals(quantity) == true) {
return
}
this.quantity = quantity
notifyPropertyChanged(BR.quantity)
}
}
class AndroidView : Fragment() {
private lateinit var binding: FragmentAddItemBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentAddItemBinding.inflate(inflater, container, false)
binding.addItem = AddItemObservable()
binding.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
Log.i("VIEW", "updated")
}
})
binding.lifecycleOwner = this
return binding.root
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<import type="tk.jonathancowling.inventorytracker.additem.AddItemObservable" />
<variable
name="addItem"
type="AddItemObservable" />
</data>
<LinearLayout 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"
android:orientation="vertical">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="@string/add_item_name"
android:inputType="textPersonName"
android:text="@={addItem.name}"
android:importantForAutofill="no"
tools:ignore="UnusedAttribute" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="@string/add_item_quantity"
android:inputType="number"
app:add_item_quantity="@={addItem.quantity}"
android:importantForAutofill="no"
tools:ignore="UnusedAttribute" />
</LinearLayout>
</layout>
为简洁起见,省略了适配器
我希望OnPropertyChangedCallback
被调用并在调用notifyPropertyChanged
时(在将文本输入EditText
之一时会记录为“更新”)。
答案 0 :(得分:1)
要使其正常工作,您需要直接在addOnPropertyChangedCallback
实例上调用Observable
。例如,
val observableItem = AddItemObservable()
binding.addItem = observableItem
observableItem.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
Log.i("VIEW", "updated")
}
})