In Android RecyclerView How to change the color of Alternate rows

前端 未结 6 2162
南笙
南笙 2021-02-05 11:45

I am new in android , recently I have learned recyclerview and i want to change the color of rows.

Example: I have 10 rows and I want to change color like

6条回答
  •  一生所求
    2021-02-05 12:11

    I believe one problem with all of these solutions is that onBindViewHolder doesn't get called in certain cases. If you use notify methods like notifyItemInserted(int position), you could potentially have rows stacked on top of each other with the same color - no good. You will need to call notifyItemChanged on every other item to re-render the background color corresponding to the new position.

    Using the re-render all method notifyDataSetChanged() will fix this (but is less efficient than only updating specific rows), and if you don't dynamically change the contents of a RecyclerAdapter while the user is on the screen, you won't have this issue.

提交回复
热议问题