ListView项目背景通过自定义select器

是否可以通过列表select器将自定义背景应用于每个Listview项目?

默认select器为state_focused="false"情况指定@android:color/transparent ,但将其更改为某个自定义drawable不会影响未选中的项目。 罗曼·盖伊似乎在这个答案中暗示这是可能的。

我目前正在通过在每个视图上使用自定义背景来实现相同的效果,并在select/聚焦/无论如何显示select器时隐藏它,但是将所有这些全部定义在一个地方会更加优雅。

作为参考,这是我用来尝试使这个工作的select器:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="false" android:drawable="@drawable/list_item_gradient" /> <!-- 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="@drawable/list_selector_background_disabled" /> <item android:state_focused="true" android:state_enabled="false" android:drawable="@drawable/list_selector_background_disabled" /> <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" /> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" /> <item android:state_focused="true" android:drawable="@drawable/list_selector_background_focus" /> </selector> 

这就是我如何设置select器:

 <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:listSelector="@drawable/list_selector_background" /> 

提前感谢任何帮助!

我一直为自己感到沮丧,终于解决了这个问题。 正如罗曼·盖伊(Romain Guy)暗示的那样,还有另一个国家, "android:state_selected" ,你必须使用。 使用状态drawable作为列表项目的背景,并使用列表的listSelector可绘制的不同状态:

list_row_layout.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:background="@drawable/listitem_background" > ... </LinearLayout> 

listitem_background.xml:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@color/android:transparent" /> <item android:drawable="@drawable/listitem_normal" /> </selector> 

包含ListView的layout.xml:

 ... <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:listSelector="@drawable/listitem_selector" /> ... 

listitem_selector.xml:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/listitem_pressed" /> <item android:state_focused="true" android:drawable="@drawable/listitem_selected" /> </selector> 

当你有一个9补丁绘制填充作为背景时,dglmtn的解决scheme不起作用。 奇怪的事情发生了,我甚至不想谈论它,如果你有这样的问题,你就知道它们。

现在,如果你想有一个不同的状态和9修补程序可绘制的列表视图(它可以与任何可绘制和颜色,我认为),你必须做2件事情:

  1. 为列表中的项目设置select器。
  2. 摆脱列表的默认select器。

你应该做的是首先设置row_selector.xml:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/list_item_bg_pressed" /> <item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/list_item_bg_focused" /> <item android:state_enabled="true" android:state_selected="true" android:drawable="@drawable/list_item_bg_focused" /> <item android:drawable="@drawable/list_item_bg_normal" /> </selector> 

不要忘记android:state_selected 。 它的工作方式类似于android:state_focused列表,但是它应用于列表项。

现在将select器应用于项目(row.xml):

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@drawable/row_selector" > ... </RelativeLayout> 

为列表制作一个透明的select器:

 <ListView android:id="@+id/android:list" ... android:listSelector="@android:color/transparent" /> 

这应该做的事情。

永远不要使用“背景颜色”您的列表视图行…

这将阻止每个select器操作(是我的问题!)

祝你好运!

Android Developers Blog中的文章“为什么我的列表是黑色的?Android优化”对滚动时列表背景为什么会变黑的问题有详细的解释。 简单的答案:将cacheColorHint设置为透明(#00000000)。

如果你放入list_row_layout.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:background="@drawable/listitem_background">... </LinearLayout> 

listitem_selector.xml:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/dark" android:state_pressed="true" /> <item android:drawable="@color/dark" android:state_focused="true" /> <item android:drawable="@android:color/white" /> </selector> 

代替:

 android:drawable="@color/transparent" 

 android:drawable="@android:color/transparent" 

你可以写一个主题:

 <pre> android:name=".List10" android:theme="@style/Theme" 

theme.xml

 <style name="Theme" parent="android:Theme"> <item name="android:listViewStyle">@style/MyListView</item> </style> 

styles.xml

  <style name="MyListView" parent="@android:style/Widget.ListView"> <item name="android:listSelector">@drawable/my_selector</item> 

my_selector是你想自定义select器我很抱歉,我不知道如何写我的代码

我不知道如何通过select器本身达到你想要的效果 – 毕竟,根据定义,整个列表中有一个select器。

然而,你可以控制select的变化,并绘制任何你想要的。 在这个示例项目中 ,我使select器透明,并在选定的项目上绘制一个条。

我总是使用相同的方法,它每一次,每一个地方工作:我只是使用像这样的select器

 <item android:state_activated="true" android:color="@color/your_selected_color" /> <item android:state_pressed="true" android:color="@color/your_pressed_color" /> <item android:color="@color/your_normal_color"></item> 

并设置ListView(这是非常重要的,使其工作)的属性

 android:choiceMode="singleChoice" 

为textColor只是放在颜色文件夹(重要的是,不可绘制的文件夹!)这样的select器

 <item android:state_activated="true" android:color="@color/your_selected_textColor" /> <item android:state_pressed="true" android:color="@color/your_pressed_textColor" /> <item android:color="@color/your_normal_textColor"></item> 

这是一个示例行模板

 <ImageView android:id="@+skinMenu/lblIcon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:src="@drawable/menu_catalog" /> <TextView android:id="@+skinMenu/lblTitle" style="@style/superlabelStyle" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:gravity="center_vertical" android:text="test menu testo" android:textColor="@color/menu_textcolor_selector" android:textSize="20dp" android:textStyle="bold" /> 

一切显示工作没有繁琐的解决方法。 希望这个帮助

FrostWire团队在这里。

所有select器废话API不能按预期工作。 在尝试所有在这个线程中呈现的解决scheme后,我们只是在膨胀ListView项目的时候解决了这个问题。

  1. 确保你的项目保持它的状态,我们做了MenuItem的成员variables(布尔select)

  2. 当你膨胀时,询问是否select了潜在的项目,如果是的话,只要设置你想要的可绘制资源作为背景(不pipe是9patch还是其他)。 确保您的适配器知道这一点,并且在select某些东西时调用notifyDataChanged()。

      @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = convertView; if (rowView == null) { LayoutInflater inflater = act.getLayoutInflater(); rowView = inflater.inflate(R.layout.slidemenu_listitem, null); MenuItemHolder viewHolder = new MenuItemHolder(); viewHolder.label = (TextView) rowView.findViewById(R.id.slidemenu_item_label); viewHolder.icon = (ImageView) rowView.findViewById(R.id.slidemenu_item_icon); rowView.setTag(viewHolder); } MenuItemHolder holder = (MenuItemHolder) rowView.getTag(); String s = items[position].label; holder.label.setText(s); holder.icon.setImageDrawable(items[position].icon); //Here comes the magic rowView.setSelected(items[position].selected); rowView.setBackgroundResource((rowView.isSelected()) ? R.drawable.slidemenu_item_background_selected : R.drawable.slidemenu_item_background); return rowView; } 

如果select器真的能够工作,这将是非常好的,理论上这是一个很好的,优雅的解决scheme,但看起来好像已经坏了。 吻。