简体   繁体   中英

ListView Multichoice listener is not working properly when we enable from ImageView

在此处输入图片说明

after releasing the long click, the item is getting deselected automatically.

holder.attach_img.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (lvChatList.isItemChecked(position))
                        lvChatList.setItemChecked(position, false);
                    else
                        lvChatList.setItemChecked(position, true);
                    notifyDataSetChanged();
                    return false;
                }
            });

As you may know, the View hierarchy in Android is represented by a tree. When you return true from the onLongClick() - it means that the View that currently received the event is the true event receiver and the event should not be propagated to the other Views in the tree; when you return false - you let the event be passed to the other Views that may consume it. So for your current scenario, just make onLongClick(View view) returns return true instead of return false and check this https://developer.android.com/reference/android/view/View.OnLongClickListener.html

@Override
    public boolean onLongClick(View view) {
        if (lvChatList.isItemChecked(position))
                    lvChatList.setItemChecked(position, false);
                else
                    lvChatList.setItemChecked(position, true);
                notifyDataSetChanged();
                return true;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM