简体   繁体   English

Android Listview项目长按不响应CheckBox事件

[英]Android Listview item long click don't respond CheckBox event

This is what I want to achieve function: The Listview item style use custom layout XML file. 这就是我要实现的功能: Listview项样式使用自定义布局XML文件。 When I long press on the listview item, the checkbox is VISIBLE and can respond to a click event. 当我长按列表视图项时,此复选框可见,并且可以响应单击事件。 在此处输入图片说明

But when I click the listview or checkbox, it doesn't have any response event.Why? 但是当我单击列表视图或复选框时,它没有任何响应事件。为什么?

This is part of my List Adapter code: 这是我的列表适配器代码的一部分:

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

This is Click Event : 这是Click事件

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

            }
        });

If i understood right, i think that you forgot to insert in the parent layout of the checkbox of List Adapter : 如果我理解正确,我认为您忘记插入“ List Adapter ”复选框的父布局中:

android:descendantFocusability="blocksDescendants">

Something like this (this is a random example so you have to follow your layout, i created this only to make you able to understand where you have to put that information): 像这样的东西(这是一个随机的示例,因此您必须遵循自己的布局,我创建它的目的只是为了使您能够理解将信息放置在何处):

<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>

如果我没记错的话,由于ListViewItem具有一个ClickEventListener,因此任何嵌入式View都不会发生单击之类的事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 listView 项目不响应单击 - listView items don't respond to click 如何使用Android listview中的复选框处理列表视图项的单击事件? - How to handle click event for a list view item with checkbox in Android listview? Android ListView在Item Long Click Release上运行事件 - Android ListView running an event on Item Long Click Release 单击ListView的列表项不响应 - Click on list item of a ListView doesn't respond 使用复选框在android listview中未触发复选框单击事件 - Checkbox click event not triggered in android listview with checkbox 单击长项目后如何在所有 listView 项目上添加复选框? 安卓Java - How to add checkbox on all the listView items after having a long item click? Android Java android - 长按从 ListView 中删除项目 - android - removing item from ListView on long click 点击Android中自定义列表视图中的复选框 - Click event from checkbox in custom listview in Android Android:Listview不会保存复选框的状态 - Android: Listview don't save a state of checkbox Android ListView标头视图无法响应单击事件,有时当标头视图通过键盘模式获得焦点时,然后在触摸模式下单击它 - Android ListView headerview can't respond to click event, sometimes when the headerview get focus by keybord mode, then click it in touch mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM