我很茫然因为我觉得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;
}
}
答案 0 :(得分:0)
将列表项的背景设为android:background="?android:attr/activatedBackgroundIndicator"
如果您仍然遇到任何问题,请尝试从listSelector
xml中删除ListView
属性
答案 1 :(得分:-1)
你可以使用 android.R.layout.simple_list_item_activated_2
mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_activated_2,
null,
new String[] { Favorite.BUS_NAME, Favorite.STATION_NAME },
new int[] { android.R.id.text1, android.R.id.text2, }, 0);
setListAdapter(mAdapter);