【问题标题】:Android: ListView selected item (onClick) stay selected after actionAndroid:ListView选中的项目(onClick)在操作后保持选中状态
【发布时间】: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


【解决方案1】:

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

或使用

改为android:listSelector="?attr/selectableItemBackground"

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

【讨论】:

  • 能否提供您的ContractListFragment的布局和代码?
  • 我在上面的帖子中添加了 fragment_list.xml、list.xml 和我的数组适配器
  • 请从您的 fragment_xml 的列表视图中删除:android:listSelector="@color/colorListSelected" 或改用android:listSelector="?attr/selectableItemBackground"
  • @yvi,对你有帮助吗?
  • 我找到了一个解决方案并将其附加到我的问题中
猜你喜欢
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
  • 2017-09-17
  • 2014-06-14
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多