开发者

Android ListView Item Colour and Scrolling issue

I have a messaging application that displays a list of received messages however I want to highlight the messages that have NOT been read with a colour(Yellow) whereas the other list items remain the default list item colour (White).

I have managed to do this using the code below however whenever I scroll the list then all of the list items regardless of whether they have been read or not will then be the "highlight" colour when they scroll out of view and then back into view.

My list selector:

<?xml vers开发者_开发百科ion="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@android:color/transparent" />
<item android:state_pressed="true" android:drawable="@android:color/transparent" />
<item android:state_selected="false" android:drawable="@drawable/messageUnreadColour" />

My code behind in my array adapter that applies the setting:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

Message_ListItem entry = items.get(position);  

   ....setup list item etc

   // Get whether the message has been read
   if (!entry.getHasBeenRead()) {

      // Set the colour to highlight the listitem
      convertView.setBackgroundResource(R.drawable.message_listitem_unread);
   }

  return convertView;
}

I have already removed the cacheColorHint setting on the list to see if this helps, but this has no effect.

Is there any way I can solve this?


You forgot the else clause in you getView() method. The list items are recycled, so once you've set the background on an unread item, that item might be reused as a read item. Do something like this:

if (!read) { setBackgroundResource(R.drawable.unread); }
else { setBackgroundResource(R.drawable.read); }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜