简体   繁体   English

RecyclerView:更改每个列表项上的Textview的背景颜色

[英]RecyclerView: Changing background color of a Textview on each List Item

I am attempting to change the background color of a textview within a list item inside a RecyclerView based on an integer value given through getMatch . 我试图根据通过getMatch给出的整数值来更改RecyclerView内列表项内textview的背景颜色。 This method returns an int value between 0-100. 此方法返回0到100之间的int值。

The code I have posted below works with the initial list items that are created, but when an item is deleted (by swiping left to right) and a new list item is created, the color is incorrect. 我在下面发布的代码适用于创建的初始列表项,但是当删除一个项(通过从左向右滑动)并创建一个新的列表项时,颜色是不正确的。 Also as more are deleted, the previous items (once correct) change color to an incorrect color. 同样,随着更多内容的删除,先前的项目(一次正确)会将颜色更改为错误的颜色。 I'm not sure what is wrong with my my methodology and would appreciate some correction and guidance on the matter. 我不确定我的方法有什么问题,希望您对此事进行一些纠正和指导。 I am hoping for a better implementation of a solution all together. 我希望可以更好地实现解决方案。

Code within my Adapter: 我的适配器中的代码:

@Override
public void onBindViewHolder(Holder holder, int position) {
    List_Item item = listData.get(position);
    holder.name.setText(item.getName());
    holder.age.setText(item.getAge());
    int match = item.getMatch();
    holder.match.setText("" + match + "%");
    TextView matchPercentage = (TextView) v.findViewById(R.id.match_text_view);
    if (match>=85){
        matchPercentage.setBackgroundResource(R.color.match_blue);
    }else if (match>=75 && match<85){
        matchPercentage.setBackgroundResource(R.color.match_green);
    }else if (match>=60 && match<75){
        matchPercentage.setBackgroundResource(R.color.match_yellow_green);
    }else if (match>=50 && match<60){
        matchPercentage.setBackgroundResource(R.color.match_yellow);
    }else if (match>=40 && match<50){
        matchPercentage.setBackgroundResource(R.color.match_orange);
    }else if(match<40 && match>=0){
        matchPercentage.setBackgroundResource(R.color.match_red);
    }

If anyone is wondering, the holder.match.setText is always the correct integer. 如果有人想知道, holder.match.setText始终是正确的整数。

Initially Correct: 最初正确:

正确

After deleting a few: 删除几个之后:

在此处输入图片说明

I suspect the issue has to do with the fact that this code is within onBind, but I'm not sure where else to implement it 我怀疑问题与该代码位于onBind内有关,但是我不确定在其他地方实现它

EDIT package andrewnguyen.finishedmoietyapp.recyclerview; 编辑包andrewnguyen.finishedmoietyapp.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import andrewnguyen.finishedmoietyapp.Global;
import andrewnguyen.finishedmoietyapp.R;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.Holder> {
    private View v;
    private List<List_Item> listData;
    private LayoutInflater inflater;

    private ItemClickCallback itemClickCallback;

    public interface ItemClickCallback {
        void onItemClick(View v, int p);

        void onSecondaryIconClick(int p);
    }

    public void setItemClickCallback(final ItemClickCallback itemClickCallback) {
        this.itemClickCallback = itemClickCallback;
    }

    public RecyclerViewAdapter(List<List_Item> listData, Context c) {
        inflater = LayoutInflater.from(c);
        this.listData = listData;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        //Sets the holder for the list items
        View view = inflater.inflate(R.layout.list_item_layout, parent, false);
        v = view;
        Global global = new Global();
        int height = global.getScreenHeight() / 5;
        int width = global.getScreenWidth() / 5;
        RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.container_of_list_item);
        rl.getLayoutParams().height = height;
        ImageView profilePic = (ImageView) view.findViewById(R.id.profile_pic_image_view);
        profilePic.getLayoutParams().width = width;
        TextView matchPercentage = (TextView) view.findViewById(R.id.match_text_view);
        matchPercentage.getLayoutParams().width = global.getScreenWidth() / 4;
        TextView fullname = (TextView) view.findViewById(R.id.name_text_view);
        fullname.setWidth(width * 4);
        fullname.setPadding(width + 20, height / 5, 0, 0);
        TextView age = (TextView) view.findViewById(R.id.age_text_view);
        age.setWidth(width * 4);
        age.setPadding(width + 20, 0, 0, 0);

        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        List_Item item = listData.get(position);
        holder.name.setText(item.getName());
        holder.age.setText(item.getAge());
        int match = item.getMatch();
        holder.match.setText("" + match + "%");
        Toast.makeText(v.getContext(), ""+ match,
                Toast.LENGTH_LONG).show();
        TextView matchPercentage = (TextView) v.findViewById(R.id.match_text_view);
        if (match>=85){//DOESN"T WORK WELL...
            matchPercentage.setBackgroundResource(R.color.match_blue);
        }else if (match>=75 && match<85){
            matchPercentage.setBackgroundResource(R.color.match_green);
        }else if (match>=60 && match<75){
            matchPercentage.setBackgroundResource(R.color.match_yellow_green);
        }else if (match>=50 && match<60){
            matchPercentage.setBackgroundResource(R.color.match_yellow);
        }else if (match>=40 && match<50){
            matchPercentage.setBackgroundResource(R.color.match_orange);
        }else if(match<40 && match>=0){
            matchPercentage.setBackgroundResource(R.color.match_red);
        }


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

    class Holder extends RecyclerView.ViewHolder implements View.OnClickListener{

        ImageView thumbnail;
        //ImageView secondaryIcon;
        TextView name;
        TextView age;
        TextView match;
        View container_of_list_item;

        public Holder (View itemView) {
            super(itemView);
            match = (TextView)itemView. findViewById(R.id.match_text_view);
            thumbnail = (ImageView)itemView.findViewById(R.id.profile_pic_image_view);
//            secondaryIcon = (ImageView)itemView.findViewById(R.id.im_item_icon_secondary);
//            secondaryIcon.setOnClickListener(this);
            age = (TextView)itemView.findViewById(R.id.age_text_view);
            name = (TextView)itemView.findViewById(R.id.name_text_view);
            container_of_list_item = itemView.findViewById(R.id.container_of_list_item);
            container_of_list_item.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.container_of_list_item){
                itemClickCallback.onItemClick(v, getAdapterPosition());
            } else {
                itemClickCallback.onSecondaryIconClick(getAdapterPosition());
            }
        }
    }
}

declare matchPercentage in ViewHolder constructor and use in onBindViewHolder as holder.matchPercentage.setBackgroundResource 在ViewHolder构造函数中声明matchPercentage,并在onBindViewHolder中用作holder.matchPercentage.setBackgroundResource

This is because Recycler view uses a ViewHolder to store references to the views for one entry in the recycler view. 这是因为Recycler视图使用ViewHolder来存储对Recycler视图中一个条目的视图的引用。 A ViewHolder class is a static inner class in your adapter which holds references to the relevant views. ViewHolder类是适配器中的静态内部类,其中包含对相关视图的引用。 With these references your code can avoid the time-consuming findViewById() method to update the widgets with new data. 使用这些引用,您的代码可以避免使用费时的findViewById()方法来用新数据更新小部件。

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

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