开发者

Setting list item background color loses highlighting

I've created an inbox Activity and I'm mirroring some functionality of the default Mail application that comes with Android.

In Mail, the background color of a message that has not been read is a lighter color than the rest of the items in the list. I've mirrored this by setting calling setBackgroundResource in the getView method of my Adapter. setBackgroundColor doesn't do anything.

        if (!msgSum.getIsRead()) {
      开发者_JAVA技巧      LinearLayout l = (LinearLayout) v.findViewById(R.id.inbox_background);
            if (l != null) {
                l.setBackgroundResource(R.color.inbox_unread);
            }
        }
        else {
            LinearLayout l = (LinearLayout) v.findViewById(R.id.inbox_background);
            if (l != null) {
                l.setBackgroundResource(R.color.inbox_read);
            }
        }

The problem is, the items then lose the ability to highlight. What I mean is, typically if you scroll your mouse wheel while using the emulator (or if your Android device has some sort of scrolling capability, you use that), the list items will turn yellow like they have focus.

How do I accomplish what I'm trying to do without losing the ability to highlight a list item?

Thanks for your help


This is because the highlight is drawn behind the list items. You need to create a state list drawable for your item's background that sets the background color to transparent in the selected state. If you lookup the talk about ListView I gave at Google I/O 2010, you will get an example of how to do this.


Per Romain Guy's suggestion, I watched the "Google I/O 2010 - The world of ListView" video on YouTube. Around minute 31:43, he posts a snippet of code that solves this problem. You must make a couple modifications, though (I'm developing against 2.1). First you must declare the xmlns:android parameter in the selector tag. In your Java code, you must call convertView.setBackgroundResource(R.drawable.yourselector), instead of setBackground (which no longer appears to exist).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜