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;
}
});
我正在尝试选择多个图像存储在其他地方。我尝试使用GridView
和ExpandableGridView
但onCreateActionMode
从未调用过。
其回调方法onItemCLick
和onItemLongClick
也无效。
如果我通过Adapter类处理onClick
,则工作正常。
如果我错过了什么,请帮忙。
答案 0 :(得分:0)
GridView
默认情况下长按其中一个项目即可启动ActionMode
。
因此,请尝试删除自定义OnItemLongClickListener
并查看是否有效。