android切换按钮不想在setChecked()中回调

时间:2013-12-18 07:22:01

标签: android android-listview

在开发Android应用时,我不想在setChecked方法中进行回调。 相反,我想使用带有切换按钮来实现此目的。

以下是我的代码: XXAdapter(ListView)

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MHolder holder;
    if (convertView == null) {
        convertView = this.inflater.inflate(layout, null);

        holder = new MHolder();
        holder.toggleButton = (ToggleButton) convertView
                .findViewById(R.id.my_toggle);
        holder.toggleButton
                .setOnCheckedChangeListener(listener);
        convertView.setTag(holder);
    }
    MParam param = params.get(position);
    holder = (MHolder) convertView.getTag();

    ToggleButton toggle = holder.toggleButton;

    toggle.setChecked(false);
    if (param.state == 2)
        toggle.setChecked(true); // here
    return convertView;
}

如何在切换按钮更改标志上设置回调方法?

2 个答案:

答案 0 :(得分:0)

如果不想采取行动,请不要使用此行

holder.toggleButton
        .setOnCheckedChangeListener(listener);

答案 1 :(得分:0)

您是否检查过setOnCheckedChangeListener

ToggleButton
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if(arg1){
                    // toggleButton is checked
                }else{
                    // toggleButton is unchecked
                }
            }
        });