Android setOnCheckedChangeListener calls again when old view comes back

前端 未结 8 1927
鱼传尺愫
鱼传尺愫 2020-12-14 06:39

I cannot solve an issue with the getGroupView-method.

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

Let say

相关标签:
8条回答
  • 2020-12-14 06:52

    Set the listener to null before calling setCheck() function, and enable it after that such as the following:

    checkBox.setOnCheckedChangeListener (null);
    checkBox.setChecked(true);
    checkBox.setOnCheckedChangeListener (this);
    

    Reference: Change Checkbox value without triggering onCheckChanged

    0 讨论(0)
  • 2020-12-14 06:55

    OnCheckedChangeListener worked for me with one if condition

    if(buttonView.isShown())
    {
        //ToDo code
    }
    
    0 讨论(0)
  • 2020-12-14 06:55

    strong textThere is many way to solved the issue

    checkBoxSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               // write here your code for example ...
                  if(isChecked){
                   // do somtheing when is checked
                       }else{
                           // do somthing when is removed the check**strong text**
                            }
    
    
            }
        });
    

    **and there is another way **

        checkBoxSelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(((CheckBox)v).isChecked()){
    //do something
                }else{
    //do something
    }
            });
    
    0 讨论(0)
  • 2020-12-14 06:59

    I was stuck with the same issue and found a way around by using on click listener

    First check if the checkbox has the onclicklistener assigned to it or not. If not that means the adapter is setting the value for the first.

    if(!holder.checkbox.hasOnClickListeners())
            {
                holder.checkbox.setChecked(data.get( position ).getSelected());
    
            }
    

    Now after checking this

    holder.checkbox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        data.get( position ).setSelected(holder.checkbox.isChecked());
                        holder.checkbox.setChecked(data.get( position ).getSelected());
    
                        Toast.makeText(getApplicationContext(),
                                data.get( position ).get_number()+" : "+position, Toast.LENGTH_LONG).show();
                    }
                });
    

    Hopefully the scrolling issue goes away. Works fine here.

    0 讨论(0)
  • 2020-12-14 07:00

    Add an additional if statement isShown() in the OnCheckedChange method and it solves the problem that the OnCheckedChangeListener gets called multiple times. There is no need to change to an onClickListener:

    @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (buttonView.isShown()) { 
            if (buttonView.isChecked()) { 
                 //Do something 
            } else { 
                 //Do something 
            } 
        } 
    }
    
    0 讨论(0)
  • 2020-12-14 07:03

    Add the below to ToggleButton layout:

    android:saveEnabled="false"
    

    This would make sure android doesn't store the check state to RestoreInstance and in turn won't cause the state change experienced by you.

    0 讨论(0)
提交回复
热议问题