我在一個損失,因爲我覺得自己像機器人應該採取的這種默認選擇行爲護理...的ListView CAB經過項目着色
我使用actionbarsherlock與ListView和有上下文操作欄出現項目長按後。我希望這些項目在長按之後突出顯示,但他們只是做初始閃爍的藍色變暗,然後恢復爲默認顏色。我錯過了什麼嗎?
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;
}
}
我不會限制自己的佈局,我有我使用的自定義列表視圖行佈局 – joepetrakovich