In Android RecyclerView How to change the color of Alternate rows

前端 未结 6 2152
南笙
南笙 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 11:58

    In the onBindViewHolder of your adapter simply get the poisition and check that whether it is even or odd. If it is even, set the background color of your layout to red else blue

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, int position) {
    
        if(position%2 == 0){
            viewHolder.containerLayout.setBackgroundColor(R.color.RED);
        } else {
            viewHolder.containerLayout.setBackgroundColor(R.color.BLUE);
    
        }}
    

提交回复
热议问题