我很茫然,因为我觉得 android 应该照顾这种默认选择行为......
我正在使用带有列表视图的 actionbarsherlock,并在长按项目后出现上下文操作栏。我希望这些项目在长按后突出显示,但它们只是最初闪烁的蓝色变暗,然后它们恢复为默认颜色。我错过了什么吗?
mHabitListView.setAdapter(mAdapter);
mHabitListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mHabitListView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
if (mHabitListView.isItemChecked(position)){
mHabitListView.setItemChecked(position, false);
} else {
mHabitListView.setItemChecked(position, true);
}
if (mHabitListView.getCheckedItemCount() > 0) {
if (mMode == null) {
mMode = startActionMode(new ModeCallback());
} else {
mMode.setTitle(mHabitListView.getCheckedItemCount() + " " + getString(R.string.cab_selected_count));
}
} else {
if (mMode != null) {
mMode.finish();
}
}
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_add_new_habit:
Habit h = new Habit("Floss", "GOOD", "", "");
mDbHelper.createHabitEntry(h);
mDbHelper.close();
Cursor cursor = mDbHelper.getAllEntries();
mAdapter.changeCursor(cursor);
break;
}
return true;
}
private final class ModeCallback implements ActionMode.Callback {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Create the menu from the xml file
MenuInflater inflater = getSupportMenuInflater();
mode.setTitle(mHabitListView.getCheckedItemCount() + " " + getString(R.string.cab_selected_count));
inflater.inflate(R.menu.list_contextual_menu, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here, you can checked selected items to adapt available actions
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Destroying action mode, let's unselect all items
for (int i = 0; i < mHabitListView.getAdapter().getCount(); i++)
mHabitListView.setItemChecked(i, false);
if (mode == mMode) {
mMode = null;
}
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
long[] selected = mHabitListView.getCheckedItemIds();
if (selected.length > 0) {
for (long id: selected) {
// Do something with the selected item
}
}
mode.finish();
return true;
}
}