问题描述
我的目标是将 2 路数据绑定材料.从我的视图模型到 MutableLiveData 的滑块视图:
My goal is to 2-way databind material.Slider view to MutableLiveData from my viewmodel:
<com.google.android.material.slider.Slider
...
android:value="@={viewmodel.fps}"
...
/>
当然不行,因为androidx.databinding库中没有Slider的数据绑定适配器
Of course, it's not working because there is no databinding adapter for Slider in androidx.databinding library
[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
But, they have one for SeekBar: /androidx/databinding/adapters/SeekBarBindingAdapter.java
据我了解,2-way 数据绑定应该只适用于progress"属性,而 1-way 数据绑定需要两个属性:onChanged"和progress"
As I understand, 2-way databinding should work only with "progress" attribute, and 1-way databinding requires two attributes: "onChanged" and "progress"
我尝试为 Slider 适配 SeekBarBindingAdapter:
I made a try to adapt SeekBarBindingAdapter for Slider:
@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
but why it looks for valueAttrChanged if I only use
android:value="@={viewmodel.fps}"
?
如果在 Slider 类中没有看到 valueAttrChanged,如何找到要添加到 BindingAdapter 的正确属性?
How do I find the right attribute to add to BindingAdapter, if I don't see valueAttrChanged in Slider class?
推荐答案
我们看看SeekBarBindingAdapter 的 setOnSeekBarChangeListener()
方法.它添加了四个不同的属性:{"android:onStartTrackingTouch", "android:onStopTrackingTouch", "android:onProgressChanged", "android:progressAttrChanged"}
但只有最后一个用于双向数据绑定.
Let's look at SeekBarBindingAdapter's setOnSeekBarChangeListener()
method. It adds four different attributes: {"android:onStartTrackingTouch", "android:onStopTrackingTouch", "android:onProgressChanged", "android:progressAttrChanged"}
but only the last one is used by two-way databinding.
但是为什么有四个属性呢?如果您查看 SeekBar 类,它有 setOnSeekBarChangeListener()
方法,它允许您设置和删除侦听器.问题是 SeekBar 只能有一个侦听器,而 listener 提供不同的回调:onProgressChanged
、onStartTrackingTouch
和 onStopTrackingTouch
.
But why there are four attributes? If you look at SeekBar class, it has setOnSeekBarChangeListener()
method which allows you to set and remove a listener. The problem is that SeekBar can only have one listener, and that listener provides different callbacks: onProgressChanged
, onStartTrackingTouch
and onStopTrackingTouch
.
SeekBarBindingAdapter
注册自己的监听器,这意味着没有人可以在不删除现有监听器的情况下注册另一个监听器.这就是为什么 SeekBarBindingAdapter
提供了 onStartTrackingTouch
、onStopTrackingTouch
和 onProgressChanged
属性,这样你就可以在不注册自己的情况下监听这些事件OnSeekBarChangeListener
.
SeekBarBindingAdapter
registers its own listener which means that no one can register another listener without removing the existing one. It's why SeekBarBindingAdapter
provides onStartTrackingTouch
, onStopTrackingTouch
and onProgressChanged
attributes, so you can listen to these events without registering your own OnSeekBarChangeListener
.
实际上 Slider
适配器可以比 SeekBarBindingAdapter
简单得多,因为 Slider 允许您使用 addOnChangeListener()
添加和删除侦听器,并且removeOnChangeListener()
.因此双向数据绑定适配器可以注册自己的侦听器,其他任何人都可以注册其他侦听器而无需删除之前的侦听器.
Actually the Slider
adapter can be much simpler than SeekBarBindingAdapter
, because the Slider allows you to add and remove listeners using addOnChangeListener()
and removeOnChangeListener()
. So a two-way databinding adapter can register its own listener and anyone else can register other listeners without removing previous ones.
它允许我们定义一个非常简洁的适配器.我创建了一个kotlin的例子,希望你能把它翻译成java:
It allows us to define a pretty concise adapter. I created a kotlin example, hope you can translate it to java:
@InverseBindingAdapter(attribute = "android:value")
fun getSliderValue(slider: Slider) = slider.value
@BindingAdapter("android:valueAttrChanged")
fun setSliderListeners(slider: Slider, attrChange: InverseBindingListener) {
slider.addOnChangeListener { _, _, _ ->
attrChange.onChange()
}
}
还有布局:
...
<com.google.android.material.slider.Slider
...
android:value="@={model.count}" />
...
您可以在这里找到完整的源代码.
You can find the full sources here.
这篇关于如何为 material.Slider 视图创建绑定适配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!