[英]Selected list item color moves on scrolling the listView in Android
在我的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位于drawable文件夹中以指定颜色。 现在的问题是,当我从列表视图中选择一个项目并滚动列表时,所选项目背景颜色也会根据滚动向下/向上移动。 请提供解决方案,以便选定的项目背景颜色保持滚动状态。
编辑:这里滚动列表视图的截图通过选择单项。蓝色保持原样
请指教。
似乎在选择器中未清除状态导致这种情况。 用一个像
<?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()方法中的颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.