3

在我的 Android 应用程序中,我使用的是 listview。列表视图的代码如下

 <ListView
    android:id="@+id/inputselect_listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
    android:footerDividersEnabled="true"
    android:headerDividersEnabled="true"
    android:listSelector="@drawable/list_selector"
    android:splitMotionEvents="true" >
 </ListView>

当用户从列表视图中选择一项时,列表项的背景颜色会根据这一行发生变化

android:listSelector="@drawable/list_selector"

编辑:list_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_selected="true"
    android:drawable="@android:color/transparent" />
<item android:state_selected="true"
    android:drawable="@android:color/transparent" />
<item android:state_pressed="true" android:state_selected="false"
    android:drawable="@android:color/transparent" />
<item android:state_selected="false"
    android:drawable="@color/blue_train" />
</selector>

list_selector.xml 位于可绘制文件夹中以指定颜色。现在的问题是,当我从列表视图中选择一项并滚动列表时,选定的项目背景颜色也会根据滚动向下/向上移动。请为我提供解决方案,以便所选项目的背景颜色保持滚动状态。

编辑:这里通过选择单个项目滚动列表视图的屏幕截图。蓝色保持原样

在此处输入图像描述

请指教。

4

3 回答 3

1

似乎选择器中的未清除状态导致此问题。使用一个喜欢

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

  <!-- Selected --> 
  <item 
    android:state_focused="true" 
    android:state_selected="false" 
    android:drawable="@drawable/focused"/> 

  <!-- Pressed -->
  <item 
    android:state_selected="true" 
    android:state_focused="false"
    android:drawable="@drawable/pressed" /> 

<!-- default -->
  <item  android:drawable="@drawable/default" /> 

</selector> 
于 2013-03-18T09:16:30.310 回答
0

在 getView() 方法中添加一行;convertView.setBackgroundResource(selectedItemInAactivityList.contains.(arrayListSetInAdaptor.get(position) ? r.drawable.selected : R.drawable.transperant));

这一行解决了你的问题

于 2013-07-17T08:30:33.110 回答
0

在 onItemClick() 方法中将单击/选定的位置存储在集合或数组中。编写代码以使用存储的位置在适配器的 getview() 方法中处理颜色。

于 2017-05-13T22:33:20.093 回答