我正在尝试实现 MultiChoiceModeListener
以在我的 fragment (v4 支持库)中使用上下文操作栏。但是我在显示已选择 ListView 中的项目时遇到问题。我期望的是所选项目被突出显示。实际发生的情况是,仅显示操作栏以指示已选择的项目数,但没有指示选择了哪个项目。
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>
那么有没有办法表明哪些项目被选中了呢?
最佳答案
在 rowselector.xml
中使用 state_activated
而不是 state_selected
:
<?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
关于android - ListView 中的选定项目未突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24095036/