4

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

GridView 设置监听器:

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

    });
4

1 回答 1

4

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

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

   }

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

于 2013-01-03T16:23:15.913 回答