简体   繁体   中英

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);
    }

}

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!

Pretty simple, use the onBindViewHolder method of your adapter, and change the color of the view (which should be referenced in your ViewHolder)

Follow this example if you are new to the Recyclerview concept.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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