GridView CAB未显示长按

时间:2015-05-15 11:33:26

标签: java android gridview contextual-action-bar onitemlongclicklistener

  gridView = (ExpandableGridview)findViewById(R.id.grid);

   gridView.setChoiceMode(ExpandableGridview.CHOICE_MODE_MULTIPLE_MODAL);
    gridView.setMultiChoiceModeListener(new ExpandableGridview.MultiChoiceModeListener() {

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {

            if(checked) {
                checkedItems.add(position);
            } else {
                checkedItems.remove(checkedItems.indexOf(position));
            }
            int checkedCount = gridView.getCheckedItemCount();
            mode.setTitle(checkedCount + " selected");

        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            Log.i("test", "onCreateActionMode");
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.menu_main, menu);
            menu.findItem(R.id.done).setVisible(false);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            menu.findItem(R.id.done).setVisible(true);
            return true;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case R.id.done:
                    Toast.makeText(MainActivity.this, "sent", Toast.LENGTH_LONG).show();
                    mode.finish();
                    return true;
                default:
                    return false;
            }
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            checkedItems.removeAll(checkedItems);
        }
    });

gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            gridView.setItemChecked(position,!checkedItems.contains(position));
            return true;
        }
    });

我正在尝试选择多个图像存储在其他地方。我尝试使用GridViewExpandableGridViewonCreateActionMode从未调用过。 其回调方法onItemCLickonItemLongClick也无效。

如果我通过Adapter类处理onClick,则工作正常。 如果我错过了什么,请帮忙。

1 个答案:

答案 0 :(得分:0)

GridView默认情况下长按其中一个项目即可启动ActionMode。 因此,请尝试删除自定义OnItemLongClickListener并查看是否有效。