Kurlicue Kurlicue - 7 months ago 41
Java Question

Android: customize a single row in RecyclerView

I have a recyclerView behanving like a list, how do I change the background color of only one view? The item I want to change the color's ID is "1", I thought about getting it in the activity by it's ID and add it a decorator, but I don't know how to, or if it's even possible.

Inside the adapter:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setEventNameName(i + "");
holder.settheColor(Color.parseColor("#000000"));

}

static int i;
class ViewHolder extends RecyclerView.ViewHolder{

public TextView eventName;
public RelativeLayout theLayout;

public ViewHolder(final View itemView) {
super(itemView);
eventName = (TextView)itemView.findViewById(R.id.eventName);
theLayout = (RelativeLayout)itemView.findViewById(R.id.backgroundevent);

theLayout.setId(++i);

theLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (getAdapterPosition()>0){
removeItem(getAdapterPosition());
}else {
addItem("");
}
}
});

}

public void settheColor(int theColor){
theLayout.setBackgroundColor(Color.parseColor(String.valueOf(theColor)));
}

public void setEventNameName(String TheEventName){
eventName.setText(TheEventName);
}

}

Answer

I feel you can do it in this way --

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    currentListItem = List.get(position);
    if(position == 0) //this will let you know the first child item
    {
       holder.textview.setBackgroundColor(Color.RED);
    }   
}

Hope this helps!