Android: ListView 选中项(onClick) 动作后保持选中状态
Android: ListView selected item (onClick) stay selected after action
我的 ListView 有问题。如果我单击一个项目,该项目将突出显示(这没关系),另一个 activity 开始编辑详细信息或删除数据集。回到我的列表后,该项目仍然被选中,即使它不再可用...
<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 文件 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>
因此列表项以我的自定义颜色突出显示,并且在选择后不会保持突出显示。
请从您的 fragment_xml 的列表视图中删除此内容:android:listSelector="@color/colorListSelected"
或使用
android:listSelector="?attr/selectableItemBackground"
改为
如果不起作用,您可能喜欢使用自己的自定义样式。
我的 ListView 有问题。如果我单击一个项目,该项目将突出显示(这没关系),另一个 activity 开始编辑详细信息或删除数据集。回到我的列表后,该项目仍然被选中,即使它不再可用...
<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 文件 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>
因此列表项以我的自定义颜色突出显示,并且在选择后不会保持突出显示。
请从您的 fragment_xml 的列表视图中删除此内容:android:listSelector="@color/colorListSelected"
或使用
android:listSelector="?attr/selectableItemBackground"
改为
如果不起作用,您可能喜欢使用自己的自定义样式。