我尝试使用setMultiChoiceModeListener
执行此操作,但无效。当我选择列表视图的多个项目时,它不会突出显示该项目。我做了很多事情,但它不起作用。看到我的代码如下。
xml文件中的Listview。
<ListView
android:id="@+id/lv_ChatMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll_msg"
android:background="?android:attr/activatedBackgroundIndicator"
android:divider="@color/main_background"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:smoothScrollbar="true"
android:stackFromBottom="true" />
这是我的MessageAdapter.java
邮件适配器。
private SparseBooleanArray SelectedItemsId;
public ArrayList<MessageList> arr_Message;
public void toggleSelection(int position) {
// TODO Auto-generated method stub
selectView(position, !SelectedItemsId.get(position));
}
private void selectView(int position, boolean value) {
// TODO Auto-generated method stub
Log.e("remove", "position-- " + position + "value-- " + value);
if (value) {
SelectedItemsId.put(position, value);
// rl_MsgItem.setBackgroundResource(R.color.outofstoke);
} else {
SelectedItemsId.delete(position);
// rl_MsgItem.setBackgroundResource(android.R.color.transparent);
notifyDataSetChanged();
}
}
public int getSelectedCount() {
Log.e("getSelectedCount",
"getSelectedCount---- " + SelectedItemsId.size());
return SelectedItemsId.size();
}
public SparseBooleanArray getSelectedIds() {
Log.e("SparseBooleanArray", "SparseBooleanArray");
return SelectedItemsId;
}
public void remove(MessageList messageList) {
// TODO Auto-generated method stub
Log.e("remove", "remove");
arr_Message.remove(messageList);
removeSelection();
}
public void removeSelection() {
Log.e("removeSelection", "removeSelection");
SelectedItemsId = new SparseBooleanArray();
notifyDataSetChanged();
}
MessageActivit.java
中的代码如下:
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
mode.getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.delete:
// Calls getSelectedIds method from ListViewAdapter Class
SparseBooleanArray selected = adapter.getSelectedIds();
// Captures all selected ids with a loop
for (int i = (selected.size() - 1); i >= 0; i--) {
if (selected.valueAt(i)) {
MessageList selecteditem = (MessageList) adapter
.getItem(selected.keyAt(i));
// Remove selected items following the ids
arr_Message.remove(selecteditem);
}
}
mode.finish();
adapter = new MessageAdapter(getApplicationContext(), arr_Message);
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// TODO Auto-generated method stub
if (checked == true) {
Log.e("Position", "" + position);
} else {
Log.e("Position", "" + position);
}
final int checkedCount = lv_ChatMsg.getCheckedItemCount();
mode.setTitle(checkedCount + " Selected Message");
adapter.toggleSelection(position);
}
答案 0 :(得分:2)
在项目xml文件中这样做。
<Linerlayout
...
android:background="?android:attr/activatedBackgroundIndicator"
... >
other your item code.
</Linerlayout>