选定列表项颜色在Android中滚动列表视图时移动

选定列表项颜色在Android中滚动列表视图时移动,android,android-listview,Android,Android Listview,在我的Android应用程序中,我使用的是listview。列表视图的代码如下所示 <ListView android:id="@+id/inputselect_listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:choiceMode="singleChoice" android:footerDividersEnable

在我的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>
编辑: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位于drawable文件夹中,用于指定颜色。 现在的问题是,当我从listview中选择一个项目并滚动列表时,所选项目的背景色也会随着滚动而向下/向上移动。 请为我提供解决方案,使所选项目的背景颜色保持滚动时的颜色

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


请注意。

选择器中的未清除状态可能导致此问题。用一个像

<?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> 

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


这一行解决了您的问题

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

似乎您使用的是state_focused而不是state_pressed show list_selector.xmlslist_selector.xml现在有问题了更新类似的问题-我更新了.xml文件,但问题尚未解决。
<?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>