0

I have the following use case: using a RecyclerView with a custom adapter for listing a simple set of data (each row has something like an icon, a title, and a description). Think of your favorite email or newsfeed app and this is most likely the same visually.

I've reached the point where I need to stylize some key elements from the list quite differently to indicate a special situation - that they are "unread" or that they are "recommended" (e.g. with a different background color, the title font becomes bold, the icon is made larger, the description is hidden, etc).

All of these variations, being purely representation/visual, I have followed the good practices and defined them as 2 different <style> blocks in the values/styles.xml file. However, I am having a hard time applying one of these 2 styles to the row in the onBindViewHolder() method of the adapter, based on some programmatical condition.

(Of course, I can go down the bad way and modify the styling programmatically by manually calling setBackgroundColor() etc on each individual View, but this seems like an ugly workaround for a seemingly trivial problem; additionally, this way we are skipping all of the benefits the .xml styling provides like resolution or locale specific styles).

Dzhuneyt
  • 8,437
  • 14
  • 64
  • 118
  • Does this previous solution work for you: https://stackoverflow.com/a/21043373 – AkiAki007 Aug 30 '17 at 19:01
  • 1
    [https://stackoverflow.com/a/26245463/5015207](https://stackoverflow.com/a/26245463/5015207) – Bö macht Blau Aug 30 '17 at 19:12
  • No, because remember that RecyclerView reuses *the same* view over and over for each row, so I instantiating a new "styled" view for each row would kill the list scrolling performance. – Dzhuneyt Aug 30 '17 at 19:33

0 Answers0