Android Listview item长按不响应CheckBox事件

Android Listview item long click don't respond CheckBox event

这是我要实现的功能: Listview 项样式使用自定义布局 XML 文件。当我长按列表视图项目时,复选框是可见的并且可以响应点击事件。

但是当我点击列表视图或复选框时,它没有任何反应event.Why?

这是我的列表适配器代码的一部分:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View view = null;
        final ViewHolder holder;
        if (null != convertView && convertView instanceof LinearLayout) {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        } else {
            view = View.inflate(notifyFragment.getActivity(),R.layout.item_notify, null);
            holder = new ViewHolder();

            holder.title = (TextView) view.findViewById(R.id.notify_title);
            holder.timestamp = (TextView) view.findViewById(R.id.notify_timestamp);
            holder.content = (TextView) view.findViewById(R.id.notify_content);
            holder.cbox = (CheckBox)view.findViewById(R.id.notify_cbox);
            holder.markFlag = (ImageView)view.findViewById(R.id.notify_markflag);

            view.setTag(holder);
        }

        Item_Notify inform_item = notify_list.get(position);

        holder.title.setText(inform_item.get_title());
        holder.timestamp.setText(inform_item.get_timestamp());
        holder.content.setText(inform_item.get_content());

        if (isShow) {
            holder.cbox.setVisibility(View.VISIBLE);
            Boolean flag = notifyFragment.recodeStatu.get(position);
            if (flag == null) {
                holder.cbox.setChecked(false);
            } else {
                holder.cbox.setChecked(flag);
            }
        } else {
            holder.cbox.setVisibility(View.GONE);
        }

        return view;
    }

    static class ViewHolder {
        TextView title;
        TextView content;
        TextView timestamp;
        CheckBox cbox;
        ImageView markFlag;
    }

这是点击事件:

listView.setOnItemLongClickListener(new OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                listAdapter.isShow = true;
                listAdapter.notifyDataSetChanged();
                ll_notify_action.setVisibility(View.VISIBLE);
                return true;
            }
        });

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (listAdapter.isShow) {
                    CheckBox cb = (CheckBox) view.findViewById(R.id.notify_cbox);
                    boolean isCheck = !cb.isChecked();
                    if (isCheck) {
                        count++;
                    } else {
                        count--;
                    }
                    btn_del.setText("Delete(" + count + ")");
                    recodeStatu.put(position, isCheck);
                    cb.setChecked(isCheck);
                } else {
                    Toast.makeText(getActivity(), "click " + position, Toast.LENGTH_SHORT).show();
                }

            }
        });

如果我没记错的话,因为 ListViewItem 有一个 ClickEventListener,所以任何嵌入式视图都不会获得诸如点击之类的事件。

如果我没理解错的话,我认为你忘记在 List Adapter 的复选框的父布局中插入:

android:descendantFocusability="blocksDescendants">

类似这样的东西(这是一个随机的例子,所以你必须遵循你的布局,我创建这个只是为了让你能够理解你必须把这些信息放在哪里):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</RelativeLayout>