简体   繁体   English

具有ActionBarSherlock的蜂窝前设备的ListView上下文操作栏

[英]ListView contextual action bar for pre honeycomb devices with ActionBarSherlock

To use contextual action bar on Android Honeycomb and later, I would normally check for the SDK_INT being 11 or later, and then proceed with mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 要在Android Honeycomb和更高版本上使用上下文操作栏,我通常会检查SDK_INT是否为11或更高版本,然后继续执行mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

Now, with ActionBarSherlock I am trying to port this to older devices. 现在,我正在尝试使用ActionBarSherlock将其移植到较旧的设备上。 Now I use CHOICE_MODE_MULTIPLE instead of CHOICE_MODE_MULTIPLE_MODAL , and inside the OnItemLongClickListener , I manage to call getSherlockActivity().startActionMode(myActionModeCallback); 现在,我使用CHOICE_MODE_MULTIPLE而不是CHOICE_MODE_MULTIPLE_MODAL ,并且在OnItemLongClickListener ,我设法调用getSherlockActivity().startActionMode(myActionModeCallback);

The thing is that, when first I long press an item in the ListView, the contextual action bar correctly shows up. 问题是,当我第一次长按ListView中的一个项目时,上下文操作栏会正确显示。

But when I click on another ListView item, the item gets selected, and right after that its click listener gets fired. 但是,当我单击另一个ListView项时,该项被选中,然后立即触发其单击侦听器。 (I use that listener, so that when the user clicks on an item, it opens up a new activity where the user can edit the item, so in this case the item gets selected and immediately this new activity gets started, and ruins my contextual action bar) (我使用了该侦听器,因此,当用户单击某个项目时,它会打开一个新的活动,用户可以在其中编辑该项目,因此在这种情况下,该项目被选中并立即开始此新活动,并破坏了我的内容动作栏)

It shouldn't be happening but I suppose I'm doing something wrong. 它不应该发生,但我想我做错了什么。 I've thought about setting the OnListItemClick listener to null until I call ActionMode.finish() where I could revert it back to the previous listener, but I believe there must be a cleaner way to do it. 我曾考虑过将OnListItemClick侦听器设置为null,直到调用ActionMode.finish()为止,在这里我可以将其恢复为以前的侦听器,但是我相信必须有一种更ActionMode.finish()方法。

As far as I can understand your question, here's a solution for you. 据我了解您的问题,这是为您提供的解决方案。

You can first check if there are selected items like 您可以先检查是否有选定的项目,例如

boolean hasCheckedItems = yourListAdapter.getSelectedCount() > 0;

Then you can check for the following conditions 然后您可以检查以下情况

if (hasCheckedItems && ActionModeObject == null)
//there are some selected items, start the ActionMode 

else if (!hasCheckedItems && ActionModeObject != null)
// there no selected items, finish the ActionMode
ActionModeObject.finish();

if (ActionModeObject != null)
//Action Button Clicked

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

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