【问题标题】:How to change a backgound of a button in recyclerview [closed]如何在recyclerview中更改按钮的背景[关闭]
【发布时间】:2021-10-10 19:51:50
【问题描述】:

我做了一个recyclerview,myitem上有一个按钮。当我点击它时,我想改变它的颜色。我该怎么做?

这是我的适配器代码:

package com.example.livraison;

    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListView;

    import java.util.ArrayList;

    public class ListAdapter extends RecyclerView.Adapter<ListViewHolder> {
    ArrayList<ListModel> data;
    Context context;

    public ListAdapter(ArrayList<ListModel> data,Context context){
        this.data = data;
        this.context = context;
    }

    @NonNull
    @Override
    public ListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = 
                LayoutInflater.from(context).inflate(R.layout.item_produit_liverer,viewGroup,false);
        return new ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ListViewHolder listViewHolder, int position) {
        listViewHolder.command_i.setText(data.get(position).getCommand_m());
        listViewHolder.name_i.setText(data.get(position).getName_m());
        listViewHolder.commune_i.setText(data.get(position).getCommune_m());
        listViewHolder.providence_i.setText(data.get(position).getProvidence_m());
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}

【问题讨论】:

标签: java android button android-recyclerview


【解决方案1】:

listViewHolder.button.setBackgroundColor(ContextCompat.getColor(context, R.color.color_name));

【讨论】:

  • 感谢您的帮助
  • 我还有一个问题,如果我单击按钮,按钮颜色将为红色,如果我再次单击按钮,颜色将为绿色,我编写此代码但它不起作用:
  • listViewHolder.p_i.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { boolean click; if (click=false){ listViewHolder.p_i.setBackground(ContextCompat.getDrawable (context,R.drawable.rouge)); return true; }else if (click=true){ listViewHolder.p_i.setBackground(ContextCompat.getDrawable(context,R.drawable.vert));
  • 返回假; } 返回真;} });
猜你喜欢
  • 2023-01-15
  • 2017-12-29
  • 2013-07-22
  • 2021-02-07
  • 2016-04-05
  • 1970-01-01
  • 2014-07-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多