java - 回收器 View : Changing background color of a Textview on each List Item

标签 java android android-recyclerview

我正在尝试根据通过getMatch给出的整数值更改RecyclerView内列表项内 TextView 的背景颜色。此方法返回 0-100 之间的 int 值。

我在下面发布的代码适用于创建的初始列表项,但是当删除某个项目(通过从左向右滑动)并创建新的列表项时,颜色不正确。此外,随着删除更多项目,之前的项目(一旦正确)的颜色会更改为不正确的颜色。我不确定我的方法有什么问题,希望得到有关此事的一些纠正和指导。我希望能够更好地实现解决方案。

我的适配器中的代码:

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

如果有人想知道,holder.match.setText 始终是正确的整数。

最初正确:

Correct

删除一些后:

enter image description here

我怀疑这个问题与此代码位于 onBind 中有关,但我不确定在哪里可以实现它

编辑 包 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());
            }
        }
    }
}

最佳答案

在 ViewHolder 构造函数中声明 matchPercentage 并在 onBindViewHolder 中使用作为holder.matchPercentage.setBackgroundResource

这是因为 Recycler View 使用 ViewHolder 来存储对回收器 View 中一个条目的 View 的引用。 ViewHolder 类是适配器中的静态内部类,它保存对相关 View 的引用。通过这些引用,您的代码可以避免使用耗时的 findViewById() 方法来使用新数据更新小部件。

关于java - 回收器 View : Changing background color of a Textview on each List Item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41077519/

相关文章:

java - 如何通过 Intent 共享多个文件?

android - android assets文件夹是文件系统中的真实文件夹吗

android - 仅在未实现 onError 时使用 RxJava 的全局错误处理解决方案

Android Recyclerview 反馈问题

java - android eclipse 中 R 的问题

java - 需要帮助理解遍历二叉搜索树方法

java - 停止后续 JOptionPane 的排队

java - elasticsearch 和 fscrawler 的 JVM 设置

android - 有什么方法可以使用 cursoradapter 在 recyclerview 中实现字母部分索引?

android - 如何在 ReccyclerView 的 onBindViewHolder 中设置文本?