android recyclerview: how do I programmatically select TextView background color?

前端 未结 2 952
一向
一向 2021-01-27 20:41

I have a RecyclerView list of CardViews. On each CardView, the user previosuly selected the \"type\" from a dropdown dialog. The type choices are \"Work\" and \"Home\". The t

相关标签:
2条回答
  • 2021-01-27 21:05

    when user click on the option, update the required object in the list, listItems. Then call notifyDataSetChanged() method in your adapter.

    0 讨论(0)
  • 2021-01-27 21:14

    Use the following to set the textColor when user selects from your two option.

    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        final ListItem listItem = listItems.get(position);
        final ItemHolder itemHolder = (ItemHolder) holder;        
        itemHolder.cardType1.setText(listItem.getType());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
        if (listItem.getType() == "Work") {
           itemHolder.cardType1.setBackgroundColor(Color.parseColor("#000000"));
        }
        else if (listItem.getType() == "Home") {
           itemHolder.cardType1.setBackgroundColor(Color.parseColor("#008080"));
        }
    }
    

    Hope this helps

    0 讨论(0)
提交回复
热议问题