Android Listview项长按不响应CheckBox事件

时间:2015-03-04 13:09:08

标签: android listview android-listview

这就是我想要实现的功能: Listview项目样式使用自定义布局XML文件。当我长按listview项时,该复选框是可见的,可以响应点击事件。 enter image description here

但是当我点击列表视图或复选框时,它没有任何响应事件。为什么?

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

@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();
                }

            }
        });

2 个答案:

答案 0 :(得分:2)

如果我理解正确,我认为您忘记插入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>

答案 1 :(得分:0)

如果我没记错,因为ListViewItem有一个ClickEventListener,任何嵌入的视图都不会获得点击等事件。