[英]Can I have a View as input to a data binding BindingAdapter on Android?
I want to establish a relationship between a ProgressBar
and a WebView
Something like this in XML: 我想在XML中建立
ProgressBar
和WebView
之间的关系:
<ProgressBar
android:id="@+id/progressBar"/>
<WebView
android:id="@+id/wvb_webView"
app:attachedProgressBar="@{@id/progressBar}"/>
With this code I can create a BindingAdapter
to do the wiring like this: 使用此代码,我可以创建一个
BindingAdapter
来进行如下连线:
@BindingAdapter({"app:attachedProgressBar"})
public static void setAttachedProgressBar(WebView webView, @IdRes int progressBarId) {
ProgressBar progBar = (ProgressBar) DataBindingUtil.getBinding(webView).getRoot().findViewById(progressBarId);
wireStuff(webView, progBar);
}
This works, but it irks me that I have to call findViewById
. 这有效,但让我觉得我必须调用
findViewById
。 Is there any way I can refactor the XML and the BindingAdapter to take a direct reference to the ProgressBar
as input instead of the @IdRes int
? 有没有什么方法可以重构XML和BindingAdapter直接引用
ProgressBar
作为输入而不是@IdRes int
?
Instead of id, directly pass ProgressBar
in your BindingAdapter
而不是id,直接在
BindingAdapter
传递ProgressBar
@BindingAdapter({"app:attachedProgressBar"})
public static void setAttachedProgressBar(WebView webView, ProgressBar progBar) {
wireStuff(webView, progBar);
}
XML code : XML代码:
<ProgressBar
android:id="@+id/progressBar"/>
<WebView
android:id="@+id/wvb_webView"
app:attachedProgressBar="@{progressBar}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.