单击其项目时,RecyclerView Cardview 项目背景颜色发生变化

RecyclerView Cardview item background color change When Click on Its Item

我试图在单击 RecyclerView 项目时更改 RecyclerView CardView 背景的颜色(绿色),当我单击 RecyclerView 的下一个项目时,上一个项目必须更改/变为其原始颜色(粉红色),并且所选项目的颜色会发生变化,即绿色。有人可以给我适当的解决方案吗?

Plss see image

我的Class-:

public class RecylerAdapter extends  RecyclerView.Adapter<RecylerAdapter.ViewHolder>
{   private boolean isSelected;
    private final static int FADE_DURATION = 500;// milliseconds
    private int lastPosition = -1;
    Context cont;
  private  String[] strname;
    private int[] icon;
    public RecylerAdapter(Context con, String[] androidNames, int[] androidIcon)
    {   cont=con;
        strname=androidNames;
        icon=androidIcon;
    }
    class ViewHolder extends RecyclerView.ViewHolder
    {   private ImageView imgView;
        private TextView txtView;
        private CardView cardView;
        private SparseBooleanArray selectedItems = new SparseBooleanArray();

        public ViewHolder(final View itemView)
        {
            super(itemView);

            imgView = (ImageView) itemView.findViewById(R.id.imageView);
            txtView = (TextView) itemView.findViewById(R.id.txt);
            cardView = (CardView) itemView.findViewById(R.id.cv12);


            itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    cardView.isSelected = !cardView.isSelected;
                    notifyDataSetChanged();
                }
            });
        }
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout,parent,false);
        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindViewHolder(ViewHolder holder, int i)
    {
        if(ViewHolder.isSelected)
        {
            holder.cardView.setBackground(Color.Green);
        }
        else{
            holder.cardView.setBackground(Color.Pink);
        }
        holder.txtView.setText(strname[i]);
        holder.imgView.setImageResource(icon[i]);
        setAnimation(holder.cardView, i);
    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setAnimation(View viewToAnimate, int position)
    {
        // If the bound view wasn't previously displayed on screen, it's animated
        if (position > lastPosition)
        {
            //animation 1
            AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
            anim.setDuration(FADE_DURATION);
            viewToAnimate.startAnimation(anim);

            //animation 2
            Animation animation = AnimationUtils.loadAnimation(cont, android.R.anim.slide_in_left);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }
        else
        {
            Animation animation = AnimationUtils.loadAnimation(cont, android.R.anim.slide_in_left);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }
    }
    @Override
    public int getItemCount()
    {
        return strname.length;
    }
    public void setSelected(boolean selection){
        this.isSelected = selection;
    }
    public boolean isSelected(){
        return isSelected;
    }
}

一切都是关于你的项目选择管理使用模型 class:

MyModel.class: 这是您用于向回收者视图显示数据列表的 class。添加一个布尔变量来进行选择和取消选择。

private boolean isSelected;

public void setSelected(boolean selection){
this.isSelected = selection;
}

public boolean isSelected(){
return isSelected;
}

现在在您的适配器中单击回收站视图的项目:

myModel = list.get(position);
myModel.isSelected = !myModel.isSelected;
notifyDataSetChanged();

在adapter的onBindViewHolder方法中

    myModel = list.get(position);
    if(myModel.isSelected){
       itemView.setBackground(Color.Green);
    }else{
       itemView.setBackground(Color.Pink);
    }

使用此逻辑并检查,如果您发现任何困难,请告诉我。

您更新的代码,因为您没有使用模型列表 class,因此您无法使用模型变量选择进行管理,请检查以下内容:

    public class RecylerAdapter extends RecyclerView.Adapter<RecylerAdapter.ViewHolder> {
    private boolean isSelected;
    private final static int FADE_DURATION = 500;// milliseconds
    private int lastPosition = -1;
    Context cont;
    private String[] strname;
    private int[] icon;
    private int selectedPosition = -1;

    public RecylerAdapter(Context con, String[] androidNames, int[] androidIcon) {
        cont = con;
        strname = androidNames;
        icon = androidIcon;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imgView;
        private TextView txtView;
        private CardView cardView;
        private SparseBooleanArray selectedItems = new SparseBooleanArray();

        public ViewHolder(final View itemView) {
            super(itemView);
            imgView = (ImageView) itemView.findViewById(R.id.imageView);
            txtView = (TextView) itemView.findViewById(R.id.txt);
            cardView = (CardView) itemView.findViewById(R.id.cv12);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectedPosition = getAdapterPosition();
                    notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindViewHolder(ViewHolder holder, int i) {
        if (selectedPosition == i) {
            holder.cardView.setBackground(Color.Green);
        } else {
            holder.cardView.setBackground(Color.Pink);
        }
        holder.txtView.setText(strname[i]);
        holder.imgView.setImageResource(icon[i]);
        setAnimation(holder.cardView, i);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setAnimation(View viewToAnimate, int position) {
        // If the bound view wasn't previously displayed on screen, it's animated
        if (position > lastPosition) {
            //animation 1
            AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
            anim.setDuration(FADE_DURATION);
            viewToAnimate.startAnimation(anim);

            //animation 2
            Animation animation = AnimationUtils.loadAnimation(cont, android.R.anim.slide_in_left);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        } else {
            Animation animation = AnimationUtils.loadAnimation(cont, android.R.anim.slide_in_left);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }
    }

    @Override
    public int getItemCount() {
        return strname.length;
    }
}

在您的适配器中 class 执行此操作。

public class MyView 扩展 RecyclerView.ViewHolder {

    public TextView textView;
    public ImageView imageView;
    public ImageView lineImageView;

    public MyView(View view) {
        super(view);

        textView = (TextView) view.findViewById(R.id.name);
        imageView = (ImageView) view.findViewById(R.id.food_category_img);
        lineImageView = (ImageView) view.findViewById(R.id.line);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                for(int i=0; i<mDataset.size();i++)
                {
                    mDataset.get(i).setSelected(false);
                }
                mDataset.get(getAdapterPosition()).setSelected(true);
                notifyDataSetChanged();
            }
        });
    }
}

在 Bind view holder 中执行此操作

   if(mDataset.get(position).isSelected()){
        itemView.Background(set color)
    }else{
       itemView.Background(set color)
    }