【问题标题】:setCardBackgroundColor on a RecyclerView with CardView使用 CardView 在 RecyclerView 上设置CardBackgroundColor
【发布时间】:2016-12-23 17:54:26
【问题描述】:

首先忽略一些不是英文的文本。好吧,我试图在按下时更改mi卡的颜色。结果是卡改变了颜色,问题是其他一些卡也改变了颜色。

我正在使用一个备用BoleanArray ->selections

这是我的适配器:

private class DotesAdapter extends RecyclerView.Adapter<DotesAdapter.DoteViewHolder> implements Filterable {

    private ListaDotes mValues;
    private DotesAdapter.DoteFilter mFilter;



    public  DotesAdapter(ListaDotes items){
        mValues=items;
        mFilter= new DotesAdapter.DoteFilter(DotesAdapter.this);


    }

    @Override
    public DotesAdapter.DoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card,parent,false);
        return new DotesAdapter.DoteViewHolder(v);
    }

    @Override
    public void onBindViewHolder(DotesAdapter.DoteViewHolder holder, int position) {

        holder.Titulo.setText(mValues.get(position).getTitulo());

        String s;
        int t = mValues.get(position).getTipo();
        if(t==0)
            s= "GENERALES";
        else
        if(t==1)s= "FORTALEZA";
        else
        if(t==2)s= "REFLEJOS";
        else
        if(t==3)s= "VOLUNTAD";
        else
        if(t==4)s= "PRECISIÓN";
        else
        if(t==5)s= "ATAQUE";
        else
            s= "SOBRENATURALES";

        holder.Tipo.setText(s);



         if(selections.get(position,false))
           holder.myBackground.setCardBackgroundColor(Color.RED);



    }

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

    @Override
    public Filter getFilter() {
        // if(doteFilter == null)
        //   doteFilter = new DoteFilter(this, listaDotes);
        return mFilter;
    }

这是我的 ViewHolder:

public   class DoteViewHolder extends RecyclerView.ViewHolder {

        TextView Titulo;
        TextView  Tipo;

        CardView myBackground;
        DotesAdapter adapter;

        public DoteViewHolder(final View itemView) {
            super(itemView);
            Titulo=(TextView) itemView.findViewById(R.id.titulo);
            Tipo=(TextView) itemView.findViewById(R.id.tipo);

            myBackground =(CardView) itemView.findViewById(R.id.card);


            itemView.setLongClickable(true);
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {


                    Intent intent = new Intent(CrearPersonajeDotes.this,DoteView.class);
                    //Bundle b = new Bundle();
                    //b=filteredList.get(getAdapterPosition()).toString();
                    YoYo.with(Techniques.Flash).duration(200).playOn(v);
                    intent.putExtra("Dote", getAdapterPosition());
                    intent.putExtra("Call",1);
                    startActivity(intent);
                    return true;
                }
            });

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//HERE I CHANGE THE COLOR OF THE CARD TO RED IF THE POSITION OF THE ARRAYS IS ON TRUE OR BLUE IN THE OTHER CASE
                    if(!selections.get(getAdapterPosition(),false)) {

                        myBackground.setCardBackgroundColor(Color.RED);
                        selections.put(getAdapterPosition(),true);
                        Dote d = listaDotes.get(getAdapterPosition());
                        MainCrearPersonaje.NPersonaje.addDote(d);
                        UpdatePDText();


                    }

                    else{

                        myBackground.setCardBackgroundColor(Color.BLUE);
                        selections.put(getAdapterPosition(),false);
                        Dote d = listaDotes.get(getAdapterPosition());
                        MainCrearPersonaje.NPersonaje.removeDote(d);
                        UpdatePDText();


                        ;


                    }

                }
            });
        }
    }

【问题讨论】:

  • 我假设您正在尝试单击单个 CardView 以使用 setCardBackgroundColor 更改其颜色,对吧?如果是这样,请在 CardView 本身上设置点击事件,而不是在 ItemView 上。
  • @NileshSingh 没关系。

标签: android android-recyclerview android-cardview cardview


【解决方案1】:

您需要onBindViewHolder() 中的代码来设置颜色。发生的情况是,当视图被回收时,它仍然具有上次使用时的颜色,因此您需要在将新数据绑定到它时正确重置颜色。

int color = selections.get(position,false) ? Color.RED : Color.BLUE;
holder.myBackground.setBackgroundColor(color);

【讨论】:

  • Thx。我也试过了,但删除了 onclick 函数上的 setBackgroundColor。
  • @IvánValdés onclick 是独立且不相关的。关键是视图在滚动离开屏幕时被回收,旧视图被重用以显示不同的项目。您需要正确处理,这意味着根据绑定到此视图的项目数据设置背景。
【解决方案2】:
//this line contains a logical error. it will evaluate to false when it 
//actually true...remove the ! and it should work fine
if(!selections.get(getAdapterPosition(),false)) 

【讨论】:

    猜你喜欢
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 2020-08-16
    • 2018-11-10
    • 2019-08-11
    相关资源
    最近更新 更多