将鼠标悬停在列表视图背景颜色上时更改它

时间:2014-10-23 06:22:11

标签: android

我'在android中开发一个包含listview.my目标的项目是当我们只触摸listview的任何一行时,或者当我们只将鼠标指针放在任何列表项上时,它的背景颜色必须改为灰色。这必须实现当我们滚动它时......任何人都可以帮我解决它。

我的代码是

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    populateListView();


private void populateListView() {
String[] str={"apple,orange,grape,strawberry,guava,pineapple,etc"};
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, R.layout.textview,str);
ListView list=(ListView)findViewById(R.id.listviewone);
list.setAdapter(adapter);
}

my activity_main.xml

 <ListView
    android:id="@+id/listviewone"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="#19bee6"
    android:listSelector="@drawable/listselect"
    android:scrollbars="none">
</ListView>

listselect.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="@drawable/orange_color"/> 

<item android:state_pressed="true" android:state_accelerated="true"     android:drawable="@drawable/red_color"/> 
<item android:state_hovered="true" android:drawable="@drawable/orange_color"/>
<item android:state_focused="true" android:drawable="@drawable/red_color"/> 
<item android:state_active="true" android:drawable="@drawable/red_color"/> 
<item android:drawable="@drawable/textgradient"/> 

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@android:color/black" android:state_enabled="false" android:state_focused="true"/>
    <item android:drawable="@android:color/holo_red_dark" android:state_pressed="true"/>
    <item android:drawable="@android:color/holo_red_dark" android:state_focused="true"/>

</selector>

您也可以查看this link