The solution to the unclickable checkbox contained in the item layout of the listview

             Recently, when I wrote the following interface in the company, I encountered a bug. The item can be clicked, but the checkbox cannot be selected.

The following is a custom layout class for an item I wrote earlier:

package com.chemanman.manager.view.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chemanman.manager.R;

/**
 * Author: dream on 2016/3/29 18:53
 * Email: [email protected]
 */
public class PaymentSingleView extends LinearLayout {

    CheckBox ivIsCheck;
    DisplayView viewWayBillNo;
    TextView tvWayBillTime;
    TextView tvFromAndTo;
    DisplayView viewPayMethordAndValue;
    TextView tvSenderAndReceiver;
    TextView tvDescriptionInfo;

    public PaymentSingleView(Context context) {
        super(context);
        initView(context);
    }

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

    public PaymentSingleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }
    private void initView(Context context) {
        // load the view
        LayoutInflater inflater = LayoutInflater.from(context);

        View convertView = inflater.inflate(R.layout.list_item_payment_nopay, this, true);
       ivIsCheck= (CheckBox) convertView.findViewById(R.id.check_payment);
        tvFromAndTo= (TextView) convertView.findViewById(R.id.from_and_to);
       tvDescriptionInfo= (TextView) convertView.findViewById(R.id.description_info);
       tvSenderAndReceiver= (TextView) convertView.findViewById(R.id.sender_and_receiver);
        tvWayBillTime= (TextView) convertView.findViewById(R.id.way_bill_time);
       viewWayBillNo= (DisplayView) convertView.findViewById(R.id.way_bill_no);
       viewPayMethordAndValue= (DisplayView) convertView.findViewById(R.id.payMethod_and_value);
    }
}


 

There is no problem with the interface display, but the checkbox cannot be selected. After checking some information, it is found that the external monitoring of the item will mask the monitoring events of the internal subcomponents.
One way is to make a custom layout class implement the Checkable interface. The code changes are as follows:
package com.chemanman.manager.view.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chemanman.manager.R;

/**
 * Author: dream on 2016/3/29 18:53
 * Email: [email protected]
 */
public class PaymentSingleView extends LinearLayout implements Checkable {

    CheckBox ivIsCheck;
    DisplayView viewWayBillNo;
    TextView tvWayBillTime;
    TextView tvFromAndTo;
    DisplayView viewPayMethordAndValue;
    TextView tvSenderAndReceiver;
    TextView tvDescriptionInfo;

    public PaymentSingleView(Context context) {
        super(context);
        initView(context);
    }

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

    public PaymentSingleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }
    private void initView(Context context) {
        // load the view
        LayoutInflater inflater = LayoutInflater.from(context);

        View convertView = inflater.inflate(R.layout.list_item_payment_nopay, this, true);
       ivIsCheck= (CheckBox) convertView.findViewById(R.id.check_payment);
        tvFromAndTo= (TextView) convertView.findViewById(R.id.from_and_to);
       tvDescriptionInfo= (TextView) convertView.findViewById(R.id.description_info);
       tvSenderAndReceiver= (TextView) convertView.findViewById(R.id.sender_and_receiver);
        tvWayBillTime= (TextView) convertView.findViewById(R.id.way_bill_time);
       viewWayBillNo= (DisplayView) convertView.findViewById(R.id.way_bill_no);
       viewPayMethordAndValue= (DisplayView) convertView.findViewById(R.id.payMethod_and_value);
    }

    @Override
    public void setChecked(boolean checked) {
        ivIsCheck.setChecked(checked);
    }

    @Override
    public boolean isChecked() {
        return ivIsCheck.isChecked();
    }

    @Override
    public void toggle() {
        ivIsCheck.toggle();
    }

}
 

    Implement the Checkable interface and set the selection mode of the checkbox (sh)

mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    

  And set the attribute in the corresponding xml layout file

android:clickable="true"
android:focusable="false"

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326999454&siteId=291194637