Enable/Disable item selection at listview in multiple choice mode

末鹿安然 提交于 2019-12-03 14:45:20

If you dig into the android sourcecode (AbsListview), you will see that setting the choiceMode to MULTIPLE_MODAL will take over the longpress. That is why your listener is never called.

You can decide whether a view is clickable by return true/false in isEnabled(position) in your adapter.

The code below only solves the part where during the actionmode, the items that are already added to the basket are not clickable.

But it should be fairly easy to just uncheck the item that is longpressed if it's not a valid item.

Hope this help!

In your MultiChoiceModeListener:

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu)
{
    this.adapter.setActionMode(true);
    return true;
}

@Override
public void onDestroyActionMode(ActionMode mode)
{
    this.adapter.setActionMode(false);
}

And then in your custom adapter:

public abstract class AbstractCollectionAdapter extends AbstractCursorAdapter
{
    private boolean isActionMode;

    public AbstractCollectionAdapter(Context context)
    {
        super(context);

        this.isActionMode = false;
    }

    @Override
    public boolean isEnabled(int position)
    {
        if (this.isActionMode)
        {
            final Object item = this.getItem(position);
            if (!item.isInBasket())
            {
                //only enable items that are not inside the basket
                return true;
            }
            //all other items are disabled during actionmode
            return false;
        }
        //no actionmode = everything enabled
        return true;
    }

    public void setActionMode(boolean isActionMode)
    {
        this.isActionMode = isActionMode;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!