关于listview的item布局中包含的checkbox无法点击的解决办法
程序员文章站
2022-05-22 22:52:46
...
最近在公司写下面一个界面时,遇到了一个bug,item可以点击,但是checkbox无法点击选中
下面是我之前写的一个item的自定义布局类:
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; /** * 作者:dream on 2016/3/29 18:53 * 邮箱:1774866723@qq.com */ 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) { // 加载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); } }
界面显示都没有问题,但是就是checkbox无法选中,查了一些资料,发现item的外部监听会掩盖内部子组件的监听事件 有一种办法是让自定义的布局类实现Checkable接口,代码更改如下:
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; /** * 作者:dream on 2016/3/29 18:53 * 邮箱:1774866723@qq.com */ 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) { // 加载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(); } }
实现Checkable接口,并设置checkbox的选择模式(sh)
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
并在对应的xml布局文件中设置属性
android:clickable="true" android:focusable="false"