【发布时间】:2017-02-28 17:18:47
【问题描述】:
我的 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 文件 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>
因此列表项以我的自定义颜色突出显示,并且在选择后不会保持突出显示。
【问题讨论】:
-
使用runnable方法,选择后(约200毫秒)去掉高光
-
这行
listView = (ListView) rootView.findViewById(android.R.id.list);有什么用? -
ListView 在我的布局文件中定义了我的 ListView。但我认为我不需要那条线,没错。
-
@Assem Mahrous,我应该在哪里插入可运行文件以及如何删除突出显示?手动覆盖颜色?
-
在您的列表中检查
标签: android listview arraylist selection