Android:ListView所选项目(onClick)在操作后保持选中状态

时间:2017-02-27 13:52:27

标签: android listview arraylist selection

我的ListView有问题。如果我单击某个项目,则突出显示该项目(这没关系),另一个活动开始编辑详细信息或删除数据集。回到我的列表后,该项目仍然被选中,即使它不再可用......

<img src="https://i.stack.imgur.com/7Xi1z.png">
<img src="https://i.stack.imgur.com/JaqCP.png">
<img src="https://i.stack.imgur.com/p3OAU.png">
here should be some pictures that i cannot insert unfortunately because the editor thinks it is code ...

只有当我使用后窄(“Vertrag”的左侧)时,该项目才不再突出显示。如果我使用另一个菜单项,该项目将保持突出显示。此外,如果我使用FloatingActionButton,我的列表中的项目将保持突出显示。 / p>

如何删除此突出显示?我已经以不同的方式尝试过clearChoices(),但它没有用。

我的fragment_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ContractList">

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@color/colorListDivider"
    android:dividerHeight="@dimen/divider_height"
    android:listSelector="@color/colorListSelected"
    android:cacheColorHint="@color/colorListcacheColorHint"
    android:drawSelectorOnTop="false" />

<TextView
    android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="@string/nodata"
    android:visibility="invisible"
    android:background="@color/colorListEmptyBackground" />

</RelativeLayout>

编辑: - - - - - - - - - - - - - - - - - - - - -

我得到了解决方案: 我设置了

android:listSelector="@drawable/list_selector"

并使用

创建了一个可绘制的xml文件list_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
    android:state_selected="false"
    android:drawable="@color/colorListBackground" />
<item android:state_pressed="true">
    <shape>
        <gradient
            android:startColor="@color/colorListSelected"
            android:endColor="@color/colorListSelected"
            android:angle="270" />
    </shape>
</item>
<item android:state_pressed="false"
    android:state_selected="true"
    android:drawable="@color/colorListBackground" />
</selector>

因此,列表项会以我的自定义颜色突出显示,并且在选择后不会保持突出显示。

1 个答案:

答案 0 :(得分:1)

请从您的fragment_xml列表视图中删除此内容:android:listSelector="@color/colorListSelected"

或使用

android:listSelector="?attr/selectableItemBackground"而不是

如果它不起作用,您可能希望使用自己的自定义样式。

相关问题