我希望能够看到在我的列表中选择了哪些项目。在将 ListView 转换为 ListFragment 之前,所选项目具有蓝色背景颜色(Holo Dark),但现在我只能在按下项目时看到蓝色 - 然后它消失了。我对项目被“检查”没有问题,因为它们是。你只是看不到它。

这是我在 onActivityCreated() 中所做的:

getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setItemsCanFocus(false);
getListView().setMultiChoiceModeListener(new LongPress());
LongPress() 是一个实现 ListView.MultiChoiceModeListener 的私有(private)类。当手指放在项目上时,它会打开一个上下文操作栏。然后用户可以选择更多项目,这就是问题发生的地方。您无法看到哪些项目被选中(我希望它们突出显示)。为什么它在扩展 ListFragment 之前有效?我试图在我的 .xml 文件中创建一个自定义选择器,这似乎也不起作用。如果有的话,我更喜欢使用自定义的。

这是我的 ListFragment 类中的另一种方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_listview, container, false);
}

fragment_listview.xml 除了改变背景颜色和分隔线的颜色外,并没有做任何特别的事情。

我应该怎么办?

我试过这个,但没有用:

在我的 list_item.xml 中:
android:background="@drawable/selector"

选择器.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
      android:drawable="@drawable/item_checked" />
</selector>

item_checked.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http//schemas.android.com/apk/res/android" >

<solid android:color="#000000" />

</shape>

最佳答案

我遇到了完全相同的问题。
我在这篇文章中找到了解决方案:Highlight custom listview item when long click
诀窍:

关于java - 突出显示 ListFragment (MultiChoiceModeListener) 中的选定项目?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12679391/

10-12 12:29
查看更多