【问题标题】:BindingAdapter for NumberpickerNumberpicker 的 BindingAdapter
【发布时间】:2019-05-07 21:15:25
【问题描述】:

我正在使用NumberPicker 实现一个“选轮器”,如this SO answer 中所述。

我还在使用数据绑定并尝试在 XML 中填充视图。

这是我所做的:

    @BindingAdapter("maxValue")
internal fun NumberPicker.customSetMaxValue(max: Int){
  maxValue = max
}

@BindingAdapter("minValue")
internal fun NumberPicker.customSetMinValue(min: Int){
  minValue = min
}

@BindingAdapter("values")
internal fun NumberPicker.customSetDisplayedValues(data: List<String>){
  displayedValues = data.toTypedArray()
}
<NumberPicker
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:entries="@array/dosage_type"
          app:maxValue="2"/>

尽管如此,我得到:error: attribute maxValue (aka com.company.project.dev:maxValue) not found

我做错了什么?

【问题讨论】:

  • 尝试去掉internal关键字。
  • @CommonsWare 试过了。不幸的是仍然不起作用。
  • OK...尝试使参数为空(Int?List&lt;String&gt;?),看看是否有帮助。我在一个正在运行的示例项目中有this
  • 它也没有用。我认为问题在于它如何绑定到 XML 中。 @CommonsWare 你如何在你的项目中做到这一点?我正在使用app:maxValue="2"

标签: android kotlin data-binding


【解决方案1】:

我正在使用 app:maxValue="2"

这不是绑定表达式。即使您的值是一个常量,您也需要将它放在绑定表达式中:

app:maxValue="@{2}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    相关资源
    最近更新 更多