Android listview no longer highlights selection onclick

a 夏天 提交于 2019-12-03 12:17:21

问题


I have a listview that was showing a yellowtint on items when I touched them. All I've done differently is change the background image in that listview xml , and now it no longer will show me the yellowtint

Here is code

the list view xml, it is just a textview with a background image:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="24sp"
    android:textColor="#000000"
    android:background="@drawable/bglistitem"
    android:gravity="center_vertical|center_horizontal">
</TextView>

the place in another layout where it is called

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="24sp"
    android:textColor="#000000"
    android:background="@drawable/bglistitem"
    android:gravity="center_vertical|center_horizontal">
</TextView>

and here is the code:

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {}
});

as you can see above, I was never doing anything that would change the default behavior of the selection highlighting, why would this be different now, perhaps something you see that I don't see?


回答1:


When you added a new background to the ListView you overrode androids default background that was most likely using a selector to tint the ListItems depending on their state.

Try to use a custom selector

Create an XML file, mycustombackground.xml, and add this to it:

<?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/item_pressed" /> 
   <item android:state_focused="true"
      android:drawable="@drawable/item_focused" /> 
   <item android:drawable="@drawable/item_normal" /> 
 </selector>

Replace the @drawables with your own in relation to the state. Then set the background of your listview to the xml file.

android:background="@drawable/mycustombackground" 

You may want to take a look into the XML to create the yellow color, or just create your own image.



来源:https://stackoverflow.com/questions/7085694/android-listview-no-longer-highlights-selection-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!