我正在尝试实现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");
}
});
}
我尝试在listview.setSelection(position)
中使用onItemCheckedStateChanged()
,但它没有任何区别。
我还尝试使用我的项目布局文件中的行选择器手动更改所选项目的背景颜色,如下所示,但这也没有任何区别:
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>
rowselector.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>
那么有没有办法说明哪些项目被选中了?
答案 0 :(得分:1)
在state_activated
中使用state_selected
代替rowselector.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@color/yellow" />
</selector>
感谢@Luksprog
答案 1 :(得分:0)
将您的选择器更改为此
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_bg_normal" android:state_selected="true"></item>
<item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/button_bg_normal"></item>
</selector>