当旧视图返回时,Android setOnCheckedChangeListener 再次调用 [英] Android setOnCheckedChangeListener calls again when old view comes back

查看:18
本文介绍了当旧视图返回时,Android setOnCheckedChangeListener 再次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解决 getGroupView 方法的问题.

I cannot solve an issue with the getGroupView-method.

问题是监听器 setOnCheckedChangeListener 被多次调用.

the problem is that the listener setOnCheckedChangeListener is getting invoked to many times.

假设我检查了某个复选框项目.然后我将它滚动出视图,然后向后滚动.发生的事情是再次调用侦听器.问题是我将 checkbox-id 存储在此侦听器内的数组列表中,以便稍后在代码中使用它.结果是每次调用侦听器时都会向数组列表中添加更多元素并扭曲数据.

Let say i check a certain checkbox-item. Then I scroll it out of view and then scroll back. What happends is that the listener is called once again. And the problem is that I store checkbox-id's in an arraylist inside this listener to use it later in the code. The consequence is that more elements is added to the arraylist everytime the listener is called and distortes the data.

有没有办法解决这个问题?我该怎么办?例如,我应该取消注册监听器吗?

Is there a solution to this? what should I do? Should I for instance unregister the listener?

@Override
 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
     View view = null;

     final int group_position = groupPosition;
     if (convertView == null) {
         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         view = inflater.inflate(R.layout.activity_phrase, parent, false);
         final ViewHolder viewHolder = new ViewHolder();
         viewHolder.text = (TextView) view.findViewById(R.id.groupTitle);
         viewHolder.checkBox = (CheckBox) view.findViewById(R.id.check);
         viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub

                if (buttonView.isChecked()) {
                    checked.add((Integer) viewHolder.checkBox.getTag());
                }
                else {
                    checked.remove((Integer) viewHolder.checkBox.getTag());
                }

            }
        });

        view.setTag(viewHolder);
        viewHolder.checkBox.setTag(groupPosition);

     } else {
         view = convertView;
         ((ViewHolder)view.getTag()).checkBox.setTag(groupPosition);
     }

     ViewHolder holder = (ViewHolder) view.getTag();
     holder.text.setText(titles[groupPosition]);

     for (int i = 0; i < checked.size(); i++) {
         if (checked.get(i) == group_position) {
             holder.checkBox.setChecked(true);
         }
         else if (checked.get(i) != group_position) {
             holder.checkBox.setChecked(false);
         }
     }

     return view;
 }

推荐答案

您使用的是哪个版本的 Android?

What version of Android are you using?

对于多个组件来说,这似乎是一个问题,尤其是使用 checkedChange() 方法(CheckBox、RadioButton)时,我无法很好地解释为什么会发生这种情况.

It seems to be an issue for multiple components, especially with a checkedChange() method (CheckBox, RadioButton) and I can't provide a good explanation why it is happening.

我会假设是因为它注册了位置状态的变化并抓取了其他属性的变化?此处

I would assume because it registers the change of the position state and grabs the change of other properties? A similar issue was addressed here

无论如何,解决此问题的方法是添加 OnClickListener().

In any case, a workaround around this could be to add an OnClickListener().

CheckBox yourCheckBox = (CheckBox) findViewById (R.id.yourId);

yourCheckBox.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
                //is chkIos checked?
        if (((CheckBox) v).isChecked()) {
                         //Case 1
        }
        else 
          //case 2

      }
    });

这篇关于当旧视图返回时,Android setOnCheckedChangeListener 再次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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