gpt4 book ai didi

android - 数据绑定(bind) : Bind single method from listener with multiple methods

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:31:13 25 4
gpt4 key购买 nike

Android UI 工具包团队的 Yigit Boyar 和 George Mount 成员给出了 talk on databinding .在 13:41 的视频中,George Mount 是这样说的

You can also do some of the weird listeners, like onTextChanged. TextWatcher has three methods on it, but everybody only cares about onTextChanged, right? You can actually access just one of them if you want, or all of them."

<Button android:onTextChanged="@{handlers.textChanged}" …/>

他似乎是在说,而不是使用通常的 addTextChangedListener看起来像这样的方法

editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//Do something
}

@Override
public void afterTextChanged(Editable editable) {}
});

我们可以简单地这样做

<EditText 
android:onTextChanged="@{handlers::onTextChanged}"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

处理程序类

public class Handlers{
public void onTextChanged(CharSequence s, int start, int before, int count) {
//Do something
}
}

该代码在这种情况下有效,但在监听器具有多种方法(如 ViewPager 的 addOnPageChangeListener)的其他情况下似乎无效。方法。例如,这不起作用

<android.support.v4.view.ViewPager
android:OnPageSelected="@{handlers::onPageSelected}"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

和处理程序类

public class Handlers{
public void onPageSelected(int position) {
//Do something
}
}

项目在尝试构建时吐出此错误

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Could not resolve handlers::onPageSelected as a listener.

****\ data binding error ****

我现在有点困惑,因为事实证明 EditText 实际上有一个继承的 onTextChanged数据绑定(bind)库还可以将 android:onTextChanged="@{handlers::onTextChanged}" 绑定(bind)到的方法。

所以我的问题是,实际上是否可以绑定(bind)到单个监听器方法,而我遗漏了一些东西,或者 George Mount 在他的演讲中是否犯了错误?

最佳答案

希望这不是谈话中的错误:-)

看起来我们没有为 ViewPager 实现它。实现起来并不太困难。有一个窍门——您必须为每个匹配方法声明一个接口(interface)。

public interface OnPageScrollStateChanged {
void onPageScrollStateChanged(int state);
}
public interface OnPageScrolled {
void onPageScrolled(int position, float offset, int offsetPixels);
}
public interface OnPageSelected {
void onPageSelected(int position);
}

然后应该为事件创建绑定(bind)适配器:

@BindingAdapter(value = {"android:onPageScrollStateChanged",
"android:onPageScrolled",
"android:onPageSelected"}, requireAll=false)
public static void setViewPagerListeners(ViewPager view,
final OnPageScrollStateChanged scrollStateChanged,
final OnPageScrolled scrolled,
final OnPageSelected selected) {
OnPageChangeListener newListener = null;
if (scrollStateChanged != null || scrolled != null || selected != null) {
newListener = new OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int state) {
if (scrollStateChanged != null) {
scrollStateChanged.onPageScrollStateChanged(state);
}
}
// similar with the other two methods ...
};
}

OnPageChangeListener oldListener = ListenerUtil.trackListener(view,
newListener, R.id.viewPagerListener);
if (oldListener != null) {
view.removeOnPageChangeListener(oldListener);
}
if (newListener != null) {
view.addOnPageChangeListener(newListener);
}
}

关于android - 数据绑定(bind) : Bind single method from listener with multiple methods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39226478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com