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