Hanged listSelector in ListView

2019-01-25 12:12发布

I am implementing ListView with android:listSelector

<style name="ListView" parent="@android:style/Widget.ListView">
    <item name="android:cacheColorHint">@color/transparent</item>
    <item name="android:divider">@drawable/divider</item>
    <item name="android:dividerHeight">1px</item>
    <item name="android:listSelector">@color/red</item>
</style>

Selecting works fine, but when I start scrolling, listSelector will randomly hang to top or bottom of ListView. I would appreciate any help.

Hanged listSelector

1条回答
We Are One
2楼-- · 2019-01-25 12:54

The main problem is that you are using a solid color instead of using Drawables. It is a drawback in the layout framework that if you set the solid colors, then the problem of hold occurs.

The code which you are using as :

<item name="android:listSelector">@color/red</item>

should be used as :

    <item name="android:listSelector">@drawable/list_view_selector</item> 

The above written drawable should be enclosed in the selector tag.

Here is the code for the list_view_selector

<?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/background_selected" />
    <item android:state_enabled="true"
     android:state_focused="true" android:drawable="@drawable/background_selected" />
    <item android:state_enabled="true"
     android:state_selected="true" android:drawable="@drawable/background_selected" />

</selector>

Note : You cannot use the solid color as it is. You have to make the selectors for the each color tone as :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:centerColor="#ff0000"
        android:endColor="#ff0000"
        android:startColor="#ff0000" />

</shape>

I have checked this at my end. Working Perfect!!

查看更多
登录 后发表回答