Android:listView Selected项目(ONCLICK)在操作后选择



我的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,列表中的项目会突出显示。<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

如何删除此突出显示?我已经以不同的方式尝试了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文件listrongelector.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>

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

请从fragment_xml的listView中删除此内容: android:listSelector="@color/colorListSelected"

或使用

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

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

最新更新