onCheckedChanged 自动调用 [英] onCheckedChanged called automatically

查看:13
本文介绍了onCheckedChanged 自动调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 recyclerview 中有一个开关,从 DB 检索数据后,数据显示在 recyclerview 中.当 recyclerview 打开时,我读取 DB,如果 DB 中的字段为Y",则启用开关,否则禁用开关.现在的问题是 onCheckedchanged 侦听器也被调用,我希望只有在用户手动设置开关时才调用 onCheckedChanged.

I have a switch in a recyclerview and data is displayed in the recyclerview after retrieving data from DB. When the recyclerview is opened I read DB and if a field in DB is "Y" I enable the switch or else I disable the switch. Now the problem is along with it the onCheckedchanged listener is also called, I want the onCheckedChanged to be called only when user sets the switch manually.

打开下面的recyclerview时执行:

On opening the recyclerview below is executed:

holder.enabledisable.setChecked(messengerRecord.get_is_valid().equalsIgnoreCase("Y"));

ViewHolder 类:

ViewHolder class:

public class viewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener{
public SwitchCompat enabledisable;
 public viewHolder(View v) {
            enabledisable = (SwitchCompat) v.findViewById(R.id.enabledisable);
            enabledisable.setOnCheckedChangeListener(this);
...................................
...................................

在刚刚打开recyclerView时调用的OncheckedChanged方法:

OncheckedChanged method which is called when the recyclerView is just opened:

@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.v("ranjith","called oncheckedchanged");
            MessengerRecord rec;
            rec = dbHelper.getRecord(descview.getText().toString());
            switch (buttonView.getId()) {
                case R.id.enabledisable:
                    if (isChecked) {
                        rec.set_is_valid("Y");
                        dbHelper.updateRecord(rec);
                     }
}

在布局文件中:

<android.support.v7.widget.SwitchCompat
    android:layout_marginRight="16dp"
    android:layout_marginEnd="16dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:id="@+id/enabledisable"
    android:layout_alignRight="@+id/textview_to"
    android:layout_alignEnd="@+id/textview_to"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"/>

推荐答案

很奇怪我们所有人都有这个问题,但 Google 没有对这个简单问题的官方回答.

It's weird all of us have this problem but not official Google answer to this simple problem.

最简单的就是检查:

buttonView.isPressed()

如果为 true,则表示用户点击了视图.

If true, means the user clicked the view.

不需要全局变量.

希望这会有所帮助.

这篇关于onCheckedChanged 自动调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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