【问题标题】:RecyclerView Cardview item background color change When Click on Its ItemRecyclerView Cardview 项目背景颜色变化当点击它的项目
【发布时间】:2016-12-02 04:40:00
【问题描述】:

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

Plss see image

我的班级-:

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

【问题讨论】:

  • 在您的模型类中添加另一个字段,例如 boolean 或 colorName,然后 onclick 项目侦听器更改该项目的颜色值
  • @Divyesh..if (selectedItems.get(getAdapterPosition(), false)) { selectedItems.delete(getAdapterPosition()); cardView.setCardBackgroundColor(Color.GREEN); } else { selectedItems.put(getAdapterPosition(), true); cardView.setCardBackgroundColor(Color.BLUE); } }我试过这个逻辑
  • 为此,您必须在 List 的 Model 类中再管理一个布尔值。检查下面的readyandroid答案。
  • 简单的解决方案是,在 onbind 方法中改变你的卡片颜色,所以不需要另一个变量

标签: android android-recyclerview android-cardview


【解决方案1】:

这完全是关于使用模型类管理您的项目选择:

MyModel.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();

在适配器的onBindViewHolder方法中

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

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

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

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

【讨论】:

  • 是的,这是您的卡片视图或您正在使用的任何东西,作为参考,我说它是 itemView。
  • @Akshay 我在检查此代码后更新了我的答案
  • @Akshay,如果它对您有用,请接受答案。
【解决方案2】:

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

公共类 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();
            }
        });
    }
}

在绑定视图持有者执行此操作

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

【讨论】:

    猜你喜欢
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多