RecyclerView Cardview项目背景颜色更改单击其项目时

时间:2016-12-02 04:40:01

标签: android android-recyclerview android-cardview

我试图在点击RecylerView项目时更改RecyclerView CardView背景的颜色(绿色),当我点击RecyclerView的下一个项目时,之前的项目必须更改/来到其原始颜色(粉红色),并选中项目颜色会改变,即绿色。有人能为我提供适当的解决方案。

Plss see image

我的班级 - :

pyserial

2 个答案:

答案 0 :(得分:7)

It's all about your item selection manage using model class:

MyModel.class: This is the class which you are using for showing list of data to recycler view. Add a boolean variable to make selection and deselection.

private boolean isSelected;

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

public boolean isSelected(){
return isSelected;
}

Now on Item click of recycler view in your adapter:

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

In onBindViewHolder method of adapter

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

Use this logic and check, if you found any difficulty let me know.

Your updated code as you are not using list of model class so you can't manage with Model variable selection, check with below:

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

答案 1 :(得分:0)

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

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