具有替代颜色和焦点颜色的Android ListView

Yog*_*esh 1 android listview colors alternate

我需要在列表视图行中设置备用颜色,但是当我这样做时,它会删除/禁用焦点默认的黄色背景

我尝试使用backgroundColor rowView.setBackgroundColor(SOME COLOR);

还有backgrounddrwable.

rowView.setBackgroundColor(R.drawable.view_odd_row_bg);
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="utf-8"?>
<selector
   xmlns:android="http://schemas.android.com/apk/res/android">
   <item
      android:state_window_focused="false"
      android:drawable="@color/odd" />

   <!--
      Even though these two point to the same resource, have two states
      so the drawable will invalidate itself when coming out of pressed
      state.
   -->
   <item
      android:state_focused="true"
      android:state_enabled="false"
      android:state_pressed="true"
      android:drawable="@color/highlight" />
   <item
      android:state_focused="true"
      android:state_enabled="false"
      android:drawable="@color/highlight" />

   <item
      android:state_focused="true"
      android:state_pressed="true"
      android:drawable="@color/highlight" />
   <item
      android:state_focused="false"
      android:state_pressed="true"
      android:drawable="@color/highlight" />

   <item
      android:state_focused="true"
      android:drawable="@color/highlight" />

</selector>
Run Code Online (Sandbox Code Playgroud)

但它不会工作.

我们有什么办法可以同时设置背景颜色和焦点颜色.

Kei*_*ith 6

我发现本教程对我很有帮助.

http://ykyuen.wordpress.com/2010/03/15/android-%E2%80%93-applying-alternate-row-color-in-listview-with-simpleadapter/

我能够使ListView项目具有交替的颜色加上焦点项目保持默认的突出显示颜色.

  • 非常迟到的评论,但对于后代:菲利普依赖于连续调用bindView,你不应该这样做.永远不要假设有关bindView调用的顺序.而是尝试对cursor.getPosition()进行模运算(游标是bindView方法的参数之一). (4认同)