我可以在Android中编辑ProgressBar吗? [英] Can i databind a ProgressBar in Android?

查看:257
本文介绍了我可以在Android中编辑ProgressBar吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在Android中使用SeekBar或ProgressBar的数据绑定吗?我有这个数据元素:

Is it possible to use data-binding for a SeekBar or ProgressBar in Android? I have this data element:

<data>
  <variable
     name="foo"
     type="com.example.Foo" />
</data>

如果我在TextField中引用变量foo.bar(这是一个int),它的工作原理如预期:

If i refer to the variable foo.bar (which is an int) in a TextField, it works as expected:

<TextView
  android:text="@{String.valueOf(foo.bar)}"
  [...]

我试图做的是这样的:

<SeekBar
  android:progress="@{foo.bar}"
  [...]

,但无法识别。 (一旦我在引号之间写了一个@,编辑器就会变红)。还有另一种数据绑定方式吗?

but it wasn't recognized. (As soon as I wrote an "@" between the quotes, it got red in the editor). Is there another way of data-binding it?

推荐答案

要在SeekBar更改时获取TextView更新,绑定到进度更改通过 android:onProgressChanged 属性。这将期望模型中的公开方法具有 SeekBarBindingAdapter.OnProgressChanged 的签名:

To get the TextView to update when the SeekBar changes, bind to progress change through the android:onProgressChanged attribute. This expects a public method in the Model with the signature of SeekBarBindingAdapter.OnProgressChanged:

查看

View

<TextView
    android:text="{@model.seekBarValue} />

<SeekBar
    android:onProgressChanged="@{model.onValueChanged}" />

模型

public class Model {
    public ObservableField<String> seekBarValue = new ObservableField<>("");

    public void onValueChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
        seekBarValue.set(progresValue + "");
    }
}

一般来说,我建议您查看源代码所有适用于数据绑定的适配器类,例如,如果您导航到Android Studio(菜单Navigate> File)中的文件 SeekBarBindingAdapter.java ,您将学习所有事件方法您可以通过适配器绑定到那里。这个特殊功能是可用的,因为Google实现了以下适配器:

In general I advice you to look at the source code for all the Adapter classes made for Data Binding. If you for instance navigate to the file SeekBarBindingAdapter.java in Android Studio (menu Navigate>File) you'll learn all the event methods you can bind to through the adapters there. This particular feature is available because Google have implemented the following adapter:

@BindingAdapter("android:onProgressChanged")
public static void setListener(SeekBar view, OnProgressChanged listener) {
    setListener(view, null, null, listener);
}

这篇关于我可以在Android中编辑ProgressBar吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆