android数据绑定:如何避免以编程方式触发的onCheckedChanged [英] android databinding: how to avoid onCheckedChanged triggered by programmatically

查看:48
本文介绍了android数据绑定:如何避免以编程方式触发的onCheckedChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在尝试在我的项目中使用android数据绑定,遇到这种问题,例如:我有3个复选框作为复选框组,如果第一个复选框被选中,则变量type 为 1.第二次使 type 变为 2,第三次使 type 变为 3.所以我是这样实现代码的.

I am now trying to use android data-binding in my project, and encounter this kind of issue, for example: I have 3 checkbox as a checkbox group, if first checkbox is checked, then a variable type is 1. the second makes type to 2, the 3rd makes type to 3. so I implement the code in this way.

   // layout.xml


   <android.support.v7.widget.AppCompatCheckBox
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:checked="@{userInfoViewModel.type == 1}"
        android:onCheckedChanged="@{(compoundButton, checked) -> userInfoViewModel.onTypeChecked(checked, 1)}"
        />

   <android.support.v7.widget.AppCompatCheckBox
        android:layout_width="50dp"
        android:layout_height="55dp"
        android:checked="@{userInfoViewModel.type == 2}"
        android:onCheckedChanged="@{(compoundButton, checked) -> userInfoViewModel.onTypeChecked(checked, 2)}"
        />

    <android.support.v7.widget.AppCompatCheckBox
        android:layout_width="50dp"
        android:layout_height="55dp"
        android:checked="@{userInfoViewModel.type == 3}"
        android:onCheckedChanged="@{(compoundButton, checked) -> userInfoViewModel.onTypeChecked(checked, 3)}"
        />

<小时>

// viewModel

public void onTypeChecked(boolean checked, int i) {
    if (checked) {
        // if it is a check. set the type
        type.set(i);
    } else {
        // if it is a uncheck. set type to unknown
        type.set(0);
    }
}

现在的问题是,如果我选中了第一个复选框,那么我选中了第二个复选框.type 应设置为 2,并且 UI 应正确更新.但实际情况是 uncheck 事件也发生在第一个复选框上,在 type 设置为 2 后,则 type.set(0) 是已触发,因此未选中复选框.

Now the problem is that, if I have checked 1st checkbox, then I check the 2nd. type should be set to 2, and the UI should update correctly. But the reality is that uncheck event also occur on the 1st checkbox, after type is set to 2, then type.set(0) is triggered, so no checkbox is checked.

事实上,这个问题与onCheckedChanged自动调用相同.我需要的是数据绑定的解决方案.

In fact, this issue is same to onCheckedChanged called automatically. What I need is a solution for data-binding.

在非数据绑定项目中,我认为最好的解决方案是使用 setCheckedSilent(@Emanuel Andrada 的回答).

In non-data-binding project, I think the best solution is using setCheckedSilent(answer by @Emanuel Andrada).

  public void setCheckedSilent(boolean checked) {
    super.setOnCheckedChangeListener(null);
    super.setChecked(checked);
    super.setOnCheckedChangeListener(listener);
}

但是在数据绑定中,我不能这样做.那么有没有专家可以帮助我?

But in data-binding, I can not do this. So is there any expert can help me out?

根据@Arpan Sharma 的回答,听onClick 而不是onCheckedChanged.这个解决方案目前有效,但我担心checked的值,它总是正确的吗?

According to @Arpan Sharma's answer, listen to onClick instead of onCheckedChanged. This solution works currently, But I am worried about the value of checked, is it always right?

public void onTypeChecked(View view, int i) {
    Boolean checked = ((CheckBox) view).isChecked();

    if (checked) {
        type.set(i);
    } else {
        type.set(0);
    }
}

推荐答案

我遇到了同样的问题,我用 onCLick 监听器代替了 onCHEcklistener .这样,当以编程方式设置时,侦听器不会更改检查状态.在您的问题中,您应该尝试为复选框设置不同的检查更改侦听器.

I faced the same problem and i used onCLick listener instead onCHeck listener .That way the listener wont change the check state when it is set programatically. In your problem you should try setting different check change listeners to your check boxes.

这篇关于android数据绑定:如何避免以编程方式触发的onCheckedChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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