Listviewリスト項目をクリックすると、項目中のButtonがクリック効果をトリガーします

1348 ワード

開発では、リスト項目にButtonが加わるのは避けられないが、buttonは通常クリックの効果がある.リスト項目をクリックするとbuttonがクリック効果をトリガーされ、表示が不合理であることがわかります.この問題をどう解決するか.
この現象が発生したのは,親リスト項目をクリックすると,応答が子に伝達されるという問題である.子クラスが親のクリックイベントに応答しなければよい.
ここでButtonクラスを書き直します.

package com.netqin.antivirus.securityreport;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

/**
 * Created with IntelliJ IDEA.
 * User: zhengdianfang
 * Date: 13-6-5
 * Time:  7:13
 */
public class SecurityReportItemButton extends Button{
    public SecurityReportItemButton(Context context) {
        super(context);
    }

    public SecurityReportItemButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SecurityReportItemButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setPressed(boolean pressed) {
        if (pressed && getParent() instanceof View
                && ((View) getParent()).isPressed()) {
            return;
        }
        super.setPressed(pressed);
    }
}