【发布时间】:2014-06-07 20:11:19
【问题描述】:
我正在尝试实现MultiChoiceModeListener 以在我的片段(v4 支持库)中使用上下文操作栏。但是我在显示已选择列表视图中的项目时遇到问题。我期望的是所选项目会突出显示。实际发生的情况是,仅显示操作栏,指示已选择的项目数,但没有指示选择了哪个项目。
if (Utils.hasHoneycomb()) {
Log.d("faizal","has honeycomb");
//Enable selection of multiple chat messages
lv_chatMessages.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
//Handle Action mode events
lv_chatMessages
.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public boolean onActionItemClicked(ActionMode arg0,
MenuItem arg1) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onCreateActionMode(ActionMode mode,
Menu menu) {
Log.d("faizal","oncreateactionmode");
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.chatsession_contextmenu, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onPrepareActionMode(ActionMode arg0,
Menu arg1) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
Log.d("faizal","onItemCheckedStateChanged : " + position);
mode.setTitle(lv_chatMessages.getCheckedItemCount() + " selected");
}
});
}
我尝试在onItemCheckedStateChanged() 中使用listview.setSelection(position),但没有任何区别。
我还尝试通过在我的项目布局文件中使用行选择器手动更改所选项目的背景颜色,如下所示,但这也没有任何区别:
chat_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id ="@+id/wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rowselector">
<TextView
android:id="@+id/txt_chat"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:focusable="false"
/>
</LinearLayout>
</LinearLayout>
行选择器.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@color/yellow" />
</selector>
那么有没有办法表明哪些项目已被选中?
【问题讨论】:
-
您需要在选择器中添加
state_activated。
标签: android