【问题标题】:Selected list item color moves on scrolling the listView in Android选定的列表项颜色在 Android 中滚动 listView 时移动
【发布时间】:2013-03-18 08:13:02
【问题描述】:

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

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

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

请指教。

【问题讨论】:

标签: android android-listview


【解决方案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> 

【讨论】:

  • 我更新了同样的.xml文件,但问题还没有解决。
【解决方案2】:

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

这一行解决了你的问题

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-31
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多