简体   繁体   English

RecyclerView onCreateViewHolder位置背景颜色

[英]RecyclerView onCreateViewHolder position background Color

I need to set background color inside onCreateViewHolder . 我需要在onCreateViewHolder设置背景颜色。 So, when my position is equal to position % 2 == 0 then set a background Color, else to set another Color. 因此,当我的position等于position % 2 == 0设置背景颜色,否则请设置其他颜色。 My background color is standard from all of my rows, that's why i thought to use it inside onCreateViewHolder and not onBindViewHolder . 我所有行的背景色都是标准色,这就是为什么我想在onCreateViewHolder而不是onBindViewHolder使用它。 Correct me if i am wrong. 如果我错了请纠正我。 The problem is that when i am using holder.getAdapterPosition inside onCreateViewHolder it returns '-1' . 问题是,当我在onCreateViewHolder使用holder.getAdapterPosition ,它返回'-1' It seems normal to me. 对我来说似乎很正常。 But how can i fix that? 但是我该如何解决呢?

  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {


    View mView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.inflate_common_item, viewGroup, false);
    ViewHolder holder= new ViewHolder(mView);
    if(holder.getAdapterPosition % 2 ==0)
    {
       //Row BackgroundColor to red.
    }
    else
    {
      //Row BackgroundColor to Green.
    }
    return holder;

} }

So when i use the code above i am getting exception that it is index out of bound. 因此,当我使用上面的代码时,我得到的例外是索引超出范围。 Is there any way to fix ? 有什么办法可以解决?

Try this: 尝试这个:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.inflate_common_item, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if(position % 2 ==0) {
        holder.itemView.setBackgroundColor(
                ContextCompat.getColor(holder.itemView.getContext(), R.color.color_red));
    } else {
        holder.itemView.setBackgroundColor(
                ContextCompat.getColor(holder.itemView.getContext(), R.color.color_green)); 
    }
}

According to ideal solution you should write this code in onBindViewHolder to use "getAdapterPosition". 根据理想的解决方案,您应该在onBindViewHolder中编写此代码以使用“ getAdapterPosition”。

if you want only different background Color of row in onCreateViewHolder than you can try this,This will give you one red color row and one other color row continuously. 如果您只希望onCreateViewHolder中行的背景颜色与您可以尝试的不同,则这将连续为您提供一个红色行和另一个其他颜色行。

you need to create a global variable. 您需要创建一个全局变量。

boolean Manual_color = true; 布尔型Manual_color = true;

@Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup,int viewType){

View mView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.inflate_common_item, viewGroup, false);
ViewHolder holder= new ViewHolder(mView);
if(Manual_color)
{
   //Row BackgroundColor to red.
   Manual_color = false;
}
else
{
  //Row BackgroundColor to Green.
  Manual_color = true;
}
return holder;

You can set color to the view that you have inflated. 您可以为已放大的视图设置颜色。

View mView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.inflate_common_item, viewGroup, false);
ViewHolder holder= new ViewHolder(mView);
if(holder.getAdapterPosition % 2 ==0) {
    mView .setBackgroundColor(ContextCompat.getColor(this, R.color.color_red));
} else {
    mView.setBackgroundColor(ContextCompat.getColor(this, R.color.color_green));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM