在多选模式下在列表视图上启用/禁用项目选择 [英] Enable/Disable item selection at listview in multiple choice mode

查看:88
本文介绍了在多选模式下在列表视图上启用/禁用项目选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多选模式下为上下文菜单注册了一个列表视图:

  private void initListViewForContextMenu(){
log.d( FilesFragment, initListViewForContextMenu());
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener(){...

问题是不是我视图中的所有项目都不应该是可选择的,只有那些显示特殊图标的项目才可以选择。我不知道如何实现,我已经定义了 OnItemLongClickListener

  getListView()。setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?>适配器,视图,int位置,长id){
Log.d( FilesFragment, OnItemLongClickListener.onItemLongClick在pos +位置);
PfmDocument doc =(PfmDocument)adapter.getItemAtPosition(position);
if(doc.isOnBasket()){
Log.d( FilesFragment, OnItemLongClickListener.onItemLongClick在购物篮中检测到);
ListView lv =(ListView)适配器;
lv.setItemChecked(position ,false);
}
返回false;
}
});

但从未调用此侦听器。



我还尝试将 OnLongClickListener 设置为适配器中的行视图,但是即使关闭了上下文菜单(不在选择模式下),也无法执行此常规单击)。

  if(doc.isOnBasket()){
rowView.setOnLongClickListener(new OnLongClickListener(){
@Override
public boolean onLongClick(View v){
return false; //不做任何事情,已经在购物篮中
}
});

//}

解决方案

如果您深入研究android源代码(AbsListview),将会看到将choiceMode设置为 MULTIPLE_MODAL 将接管长按。这就是为什么您的监听器永远不会被调用的原因。



您可以通过在 isEnabled(position)中返回true / false来确定视图是否可单击。 code>在您的适配器中。



下面的代码仅解决在操作模式下无法单击已添加到购物篮中的项目的部分。 / p>

但如果它不是有效项目,只需取消选中长按的项目就应该相当容易。



希望有帮助!



在您的MultiChoiceModeListener中:

  @Override 
public boolean onCreateActionMode(ActionMode模式,菜单菜单)
{
this.adapter.setActionMode(true);
返回true;
}

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

然后在您的自定义适配器中:

 公共抽象类AbstractCollectionAdapter扩展了AbstractCursorAdapter 
{
private boolean isActionMode;

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

this.isActionMode = false;
}

@Override
public boolean isEnabled(int position)
{
if(this.isActionMode)
{
最终对象项= this.getItem(position);
if(!item.isInBasket())
{
//仅启用不在购物篮中的项目
返回true;
}
//所有其​​他项在操作模式期间都被禁用
return false;
}
//没有动作模式=启用的所有功能
返回true;
}

public void setActionMode(boolean isActionMode)
{
this.isActionMode = isActionMode;
}
}


I have a listview registered for context menu in multiple choice mode:

private void initListViewForContextMenu(){
    log.d("FilesFragment", "initListViewForContextMenu()");
    ListView listView = getListView();
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    listView.setMultiChoiceModeListener(new MultiChoiceModeListener() { ...

The problem is that not all the items of my view should be selectable, only those showing a special icon should be available for selection. I don't know how to implement this, I've defined an OnItemLongClickListener:

getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> adapter, View view, int position, long id) {
        Log.d("FilesFragment", "OnItemLongClickListener.onItemLongClick at pos " + position);
        PfmDocument doc = (PfmDocument)adapter.getItemAtPosition(position);
        if (doc.isOnBasket()){
            Log.d("FilesFragment", "OnItemLongClickListener.onItemLongClick detected in basket");
            ListView lv = (ListView) adapter;
            lv.setItemChecked(position, false);
        }
        return false;
        }
    }); 

but this listener is never called.

I've also tried to set an OnLongClickListener to the row view in the adapter, but doing this normal click is also disable even when context menu is closed (not in selection mode).

if (doc.isOnBasket()){
    rowView.setOnLongClickListener(new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        return false; // do nothing, already in basket
    }
});

// }

解决方案

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

这篇关于在多选模式下在列表视图上启用/禁用项目选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆