在Android中发生OnClick事件时激活CAB菜单

时间:2013-01-02 20:17:11

标签: android android-actionbar android-gridview android-menu contextual-action-bar

从ActionBar单击MenuItem时尝试激活CAB菜单。以下是我如何设置GridView以监听Multi Choice。当我长按GridView中的任何项目时,multiModeChoiceListener工作正常。它工作正常。现在,当按下操作栏中的菜单项时,我需要激活CAB菜单。按下后,CAB菜单应显示已选择0项。之后,它应该允许我通过单击从GridView中选择项目。我该如何实现这个功能?

GridView set listener:

gv.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL); 
gv.setMultiChoiceModeListener(new MultiChoiceModeListener());

MultiChoiceModeListener.java

public class MultiChoiceModeListener implements
    GridView.MultiChoiceModeListener {  

public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    mode.getMenuInflater().inflate(R.menu.featured_multiselect, menu);
    MenuItem mi = menu.findItem(R.id.close);
    mi.setIcon(R.drawable.cancel);
    mode.setTitle("Select Items");
    return true;
}

public boolean onPrepareActionMode(ActionMode mode, Menu menu) {    
    return true;
}

public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    Toast.makeText(getApplicationContext(), item.getTitle(),
            Toast.LENGTH_SHORT).show();
    if (item.getTitle().toString().equalsIgnoreCase("Close")) {
        mode.finish();
    }
    return true;
}

public void onDestroyActionMode(ActionMode mode) {
    new ChangeNotifier().changeOnFavoriteStore = true;
    new AddFavorites().execute("add", device_id, dataArray);
    if (notify == true) {
        Toast.makeText(getApplicationContext(),
                "Selected items are added to Favorites",
                Toast.LENGTH_SHORT).show();
        notify = false;
    }
}

public void onItemCheckedStateChanged(ActionMode mode, int position,
        long id, boolean checked) {
    int selectCount = gridView.getCheckedItemCount();
    if (selectCount > 0) {
        notify = true;              
        dataArray.add(position);
        switch (selectCount) {
        case 1:
            mode.setSubtitle("One item added to favorites");
            break;
        default:
            mode.setSubtitle("" + selectCount
                    + " items added to favorites");
            break;
        }
    } 

   }

OnMenuItemClick方法:

 public boolean onPrepareOptionsMenu(final Menu menu) {

    final MenuItem editItem = menu.findItem(R.id.editit);

    editItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            //the CAB menu should be activated here. So that it reads that 0 items are selected in ActionBar

            return false;
        }

    });

1 个答案:

答案 0 :(得分:4)

根据您的问题,我了解您正在尝试通过单击其中一个菜单项来启动GridView关联的CAB。我不知道你是否可以这样做(但我可能会弄错)因为MultiChoiceModeListener期望检查项目是否开始。根据您的布局和GridView的整体外观,我认为您可以在GridView的末尾有一个虚拟项目(作为适配器中的额外项目)(没有显示内容)和使用setItemChecked(dummyItemPosition, true)启动GridView CAB。当然,您需要有额外的逻辑来处理MultiChoiceModeListener中的额外项目:

 public void onItemCheckedStateChanged(ActionMode mode, int position,
        long id, boolean checked) {
    if (position == theDummyPosition)
         return; // so we start the CAB but there aren't any items checked
    }
    int selectCount = gridView.getCheckedItemCount();
    if (selectCount > 0) {
        notify = true;              
        dataArray.add(position);
        // if you select another item you'll have two selected items(because of the dummy item) so you need to take care of it 
        switch (selectCount) {
        case 1:
            mode.setSubtitle("One item added to favorites");
            break;
        default:
            mode.setSubtitle("" + selectCount
                    + " items added to favorites");
            break;
        }
    } 

   }

上面的解决方案是一个黑客攻击,很可能更容易丢失MultiChoiceModeListener并简单地启动一个ActionMode,你可以在两种情况下操作。