In Android RecyclerView How to change the color of Alternate rows

前端 未结 6 2126
南笙
南笙 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);
    
        }}
    
    0 讨论(0)
  • 2021-02-05 12:02

    You can change the color of alternate row by adding the following code on your Adapter class. You can also change the images of your row by using this code.

    Put this code inside OnBindViewHolder in Adapter Class.

     if(position %2 == 1)
        {
            holder.itemView.setBackgroundColor(Color.parseColor("#FFFFFF"));
            //  holder.imageView.setBackgroundColor(Color.parseColor("#FFFFFF"));
        }
        else
        {
           holder.itemView.setBackgroundColor(Color.parseColor("#FFFAF8FD"));
           //  holder.imageView.setBackgroundColor(Color.parseColor("#FFFAF8FD"));
        }
    
    0 讨论(0)
  • 2021-02-05 12:09

    Here is a solution :

    LinearLayout ll_search =(LinearLayout)convertView.findViewById(R.id.ll_search);
    
    if(position %2 == 1) {
      ll_search.setBackgroundColor(Color.parseColor("#FFFFFF")); 
    } else { 
      ll_search.setBackgroundColor(Color.parseColor("#d3d3d3")); 
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-05 12:16

    With CardView in Kotlin

     internal fun bind(d: Detalle, position: Int, listener: OnItemClickListener) {
    
            if (position % 2 == 1) {
                cardViewPrincipal.setCardBackgroundColor(ContextCompat.getColor(itemView.context, R.color.blue_logo))
            } else {
                cardViewPrincipal.setCardBackgroundColor(ContextCompat.getColor(itemView.context, R.color.colorWhite))
            }
    

    Multiples Colors

    when {
    p % 4 == 0 -> cardViewPrincipal.setCardBackgroundColor(ContextCompat.getColor(itemView.context, R.color.yellow)) 
    p % 4 == 1 -> cardViewPrincipal.setCardBackgroundColor(ContextCompat.getColor(itemView.context, R.color.green)) 
    p % 4 == 2 -> cardViewPrincipal.setCardBackgroundColor(ContextCompat.getColor(itemView.context, R.color.blue)) 
    p % 4 == 3 -> cardViewPrincipal.setCardBackgroundColor(ContextCompat.getColor(itemView.context, R.color.red)) 
    }
    
    0 讨论(0)
  • 2021-02-05 12:20

    Using Kotlin

        if (position % 2 == 1) {
            holder?.itemView?.setBackgroundColor(context.resources.getColor(R.color.text_gray))
        } else {
            holder?.itemView?.setBackgroundColor(context.resources.getColor(R.color.white))
        }
    
    0 讨论(0)
提交回复
热议问题