android - 将鼠标悬停在 ListView 上时更改 ListView 的背景颜色

标签 android

我在 android 中开发一个项目,其中包含一个 ListView 。我的目标是当我们触摸 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);
}

我的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>

列表选择.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"/> 

最佳答案

你可以试试这个:

<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 :

关于android - 将鼠标悬停在 ListView 上时更改 ListView 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26522321/

相关文章:

java - 无服务器群组消息传递(聊天)Xmpp

android - 如何更改 timepickerdialog 中“确定”和“取消”按钮布局的背景颜色

Android:更改启动器 Activity 的正确方法(完整阅读)

android - Cordova 应用程序上的 NullPointerException

java - 无法在 Android 的自定义布局中使用 OnClickListener?

android - 第二个 Action 的 PendingIntent 会覆盖第一个 Action 和通知的 contentIntent

android - 我应该用什么替换已弃用的 Facebook InterstitialAd setAdListener?

android - 实现 Google Play 结算的智能方法

android - Android Studio:由于gradle分发网址错误而无法构建应用

java - 有目的地将 Activity 放在首位