当我点击ListView的项目时,我试图插入一个Contextual Action Bar。我想要做的是在列表中选择多个项目,然后通过单击操作栏中的相应图标对它们执行操作。我写了这段代码。
applicationListView = (ListView) findViewById(R.id.list);
applicationListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
applicationListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
SparseBooleanArray positions = applicationListView.getCheckedItemPositions();
int counter = 0;
if (positions != null) {
int length = positions.size();
for (int i = 0; i < length; i++) {
if (positions.get(positions.keyAt(i))) {
counter++;
Toast.makeText(getBaseContext(), ""+counter++, Toast.LENGTH_SHORT).show();
}
}
}
startSupportActionMode(mActionModeCallback);
}
});
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback(){
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.contextual_action_bar, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item1:
return true;
case R.id.menu_item2:
//close the action mode
//mode.finish();
return true;
default:
mode.finish();
return false;
}
}
@Override
public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
// TODO Auto-generated method stub
arg0.setTitle("Selected item: ");
return false;
}
};
存在各种问题。
1-当我点击某个项目出现在上下文ActionBar中但所选项目未突出显示时,例如颜色为蓝色。当您选择项目就像再次重新创建ActionBar一样,因为我看到ActionBar图标消失并重新出现,就好像它确实被重新创建一样。
2-当我点击吐司中的项目时,始终可视化数字1.为什么?如何获取所选项目的编号,然后将其作为操作栏的标题?