简体   繁体   English

Android:在RecyclerView中自定义一行

[英]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? 我有一个像列表一样的recyclerView,如何更改仅一个视图的背景颜色? 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. 我要更改颜色ID的项目是“ 1”,我考虑过通过ID将其添加到活动中并添加一个装饰器,但我不知道如何操作,或者甚至不可能。

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) 非常简单,使用适配器的onBindViewHolder方法,并更改视图的颜色(应在ViewHolder中引用)

Follow this example if you are new to the Recyclerview concept. 如果您不熟悉Recyclerview概念,请遵循此示例

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

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