Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

android - Hanged listSelector in ListView

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...