我的目标是 2 路数据绑定材料。从我的视图模型到 MutableLiveData 的滑块视图:
<com.google.android.material.slider.Slider
...
android:value="@={viewmodel.fps}"
...
/>
当然,它不起作用,因为 androidx.databinding 库中没有 Slider 的数据绑定适配器
[databinding] Cannot find a getter for <com.google.android.material.slider.Slider android:value> that accepts parameter type <java.lang.Integer>. If a binding adapter provides the getter, check that the adapter is annotated correctly and that the parameter type matches.
但是,他们有一个用于 SeekBar:/androidx/databinding/adapters/SeekBarBindingAdapter.java
据我了解,2 向数据绑定应该只适用于“progress”属性,而 1 向数据绑定需要两个属性:“onChanged”和“progress”
我尝试为 Slider 调整 SeekBarBindingAdapter:
@InverseBindingMethods({
@InverseBindingMethod(type = Slider.class, attribute = "android:value"),
})
public class SliderBindingAdapter {
@BindingAdapter("android:value")
public static void setValue(Slider view, int value) {
if (value != view.getValue()) {
view.setValue(value);
}
}
@BindingAdapter(value = {"android:valueAttrChanged", "android:onValueChange"}, requireAll = false)
public static void setOnSliderChangeListener(Slider view, final Slider.OnChangeListener valChanged, final InverseBindingListener attrChanged) {
if (valChanged == null)
view.addOnChangeListener(null);
else
view.addOnChangeListener((slider, value, fromUser) -> {
if (valChanged != null)
valChanged.onValueChange(slider, value, fromUser);
});
if (attrChanged != null) {
attrChanged.onChange();
}
}
@Override
public void onValueChange(@NonNull Slider slider, float value, boolean fromUser) {
}
它不是在建造:
Could not find event android:valueAttrChanged on View type Slider
但是如果我只使用它为什么会寻找 valueAttrChanged
android:value="@={viewmodel.fps}"
?
如果在 Slider 类中看不到 valueAttrChanged,如何找到要添加到 BindingAdapter 的正确属性?