我在ListView上实现了CAB和MultiChoiceModeListener
,除了在选择时更改列表项的背景颜色(多选)时,一切都很完美。这是我的代码:
listView.xml:
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#ebebeb"
android:dividerHeight="1dip"
android:listSelector="@drawable/list_selector"/>
list_selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@android:color/holo_green_light"/>
<item android:state_pressed="true" android:drawable="@android:color/holo_blue_light"/>
<item android:state_focused="true" android:drawable="@android:color/holo_blue_dark"/>
<item android:drawable="@android:color/white"/>
</selector>
片段:
l.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
l.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(android.view.ActionMode mode, int position, long id, boolean checked) {
int checkedCount = l.getCheckedItemCount();
mode.setTitle(checkedCount + " Selected");
}
@Override
public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
return true;
}
@Override
public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
}
@Override
public void onDestroyActionMode(android.view.ActionMode mode) {
}
});
答案 0 :(得分:0)
我所做的是保存有关项目的数据,看它是否被选中,然后只需调用适配器的xcdatamodel
再次创建视图。在我的情况下,它是关于更改项目列表中的图标。
这是相关的notifyDataSetChanged
部分:
MultiChoiceModeListener
这就是我在适配器中所做的:
mylist.setMultiChoiceModeListener(new MultiChoiceModeListener()
{
private int numberOfSelected = 0;
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
{
// Here you can do something when items are
// selected/de-selected,
// such as update the title in the CAB
if (checked)
{
numberOfSelected++;
} else
{
numberOfSelected--;
}
mode.setTitle(numberOfSelected + " items selected");
itemsData.get(position).isChecked = checked; //a collection of data item's status
m_listAdapter.notifyDataSetChanged();
}
...
所以我只是在选择一个项目后再次构建列表项。