How to highlight ListView item on touch?

后端 未结 4 1580
野的像风
野的像风 2021-02-02 17:46

I have a simple ListView and I want each of it items to be highlighted on user\'s touch. I thought this should happen by default but it isn\'t. Can you advice?

4条回答
  •  面向向阳花
    2021-02-02 18:37

    You may want to post your actual row layout code, but I suspect the problem will be that you set a background color on your list row. By default, the selectors are drawn behind the list items (which looks nicer, since the highlight color is behind the text). Either don't set a background color on your list items, or set it to draw the selector on top instead with ListView's drawSelectorOnTop XML attribute.

    EDIT: If you really must have an opaque background for the default state and don't want to use drawSelectorOnTop, you can also try this: Set a background on your list rows, but use a StateListDrawable to use @android:drawable/list_selector_background for all but the default state (you can define an xml file in your drawables folder for this; see the StateList documentation).

    You could also nest a layout inside your outer backgrounded row layout with its background set to @android:drawable/list_selector_background; that way the background would draw on top of your background, but below the content.

提交回复
热议问题