如何将onBindViewHolder与RecyclerView的onClick一起使用?

时间:2019-02-04 11:48:15

标签: android android-recyclerview

我有一个带有字符串和图像列表的RecyclerView。但是我需要在单击图像时更改图像并更改TextView的颜色,例如此视频(由我录制)It was supposed to be like that. but when I tap continue button they not select nothing,此SetOnclickListener位于onBindViewHolder内部(我认为这是问题吗?)。

但是在我的RecyclerView上,他们已经有一个Onclick方法,可以“操纵” Like this example,但是我不知道如何在每个位置使用.equals.valueAt来实现。清单(54个项目)。我只是想知道如何与onClick一起使用onBindViewHolder

MY RecyclerView.java我在此代码上测试了很多东西,所以...不要着急,对此感到抱歉,由于我通过了限制,哈哈我取消了大部分职位

对第一个位置的评论是因为当我取消注释此代码时,他们会更改图像和文本颜色,但没有选择其他活动,请参见第一个视频!

public class RecyclerViewAdapter2 extends RecyclerView.Adapter<RecyclerViewAdapter2.ViewHolder> {

ArrayList<Categorias> TagsDaMarca;//change like this
ArrayList<Categorias> SubServicoTagsDaMarca;//change like this
ArrayList<Categorias> DICategorias;//change like this
ArrayList<String> DISubCategorias;

boolean test = false;
HashMap<Integer, List<String>> ListaCatSubCat = new HashMap<Integer, List<String>>();
public static SparseBooleanArray sparseBooleanArray;
// for identifying: in list which items are selected
OnRecyclerViewItemClickListener listener;
int meutotal = 0;


public RecyclerViewAdapter2(ArrayList<Categorias> TagsDaMarca1, OnRecyclerViewItemClickListener listener) {
    TagsDaMarca = TagsDaMarca1;
    SubServicoTagsDaMarca = TagsDaMarca1;
    DICategorias = TagsDaMarca1;

    sparseBooleanArray = new SparseBooleanArray();
    this.listener = listener;
}


@Override
public RecyclerViewAdapter2.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_marca_tag_servico_layout, parent, false);
    return new RecyclerViewAdapter2.ViewHolder(view);
}

@Override
public void onBindViewHolder(final RecyclerViewAdapter2.ViewHolder holder, int position) {

    Categorias item = DICategorias.get(position); //change like this

    if (position <= 0) {
//            holder.itemTextView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    test = !test; //Switch boolean value
//                    holder.itemTextView.setFocusable(test);
//                    if (test == true) {
//                        holder.itemTextView.setTextColor(Color.rgb(236, 0, 140));
//                        holder.DICategoriasIcones.setImageResource(R.drawable.sub_categorias_acessorios);
//                    } else {
//                        holder.DICategoriasIcones.setImageResource(R.drawable.categorias_acessorios);
//                        holder.itemTextView.setTextColor(Color.rgb(190, 190, 190));
//                    }
//                }
//            });
        holder.itemTextView.setText(item.getCategoria());
        holder.DICategoriasIcones.setImageResource(item.getImagem());
    } else if (position <= 1) {
        holder.DICategoriasIcones.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                test = !test; //Switch boolean value
                holder.itemTextView.setFocusable(test);
                if (test == true) {
                    holder.itemTextView.setTextColor(Color.rgb(236, 0, 140));
                    holder.DICategoriasIcones.setImageResource(R.drawable.sub_categorias_animais);
                } else {
                    holder.DICategoriasIcones.setImageResource(R.drawable.categorias_animais);
                    holder.itemTextView.setTextColor(Color.rgb(190, 190, 190));
                }

                if (sparseBooleanArray.equals(1)) {
                    if (meutotal < 4) {
                        test = !test; //Switch boolean value
                        holder.itemTextView.setFocusable(test);
                        if (test == true) {
                            holder.itemTextView.setTextColor(Color.rgb(236, 0, 140));
                            holder.DICategoriasIcones.setImageResource(R.drawable.sub_categorias_animais);
                        } else {
                            holder.DICategoriasIcones.setImageResource(R.drawable.categorias_animais);
                            holder.itemTextView.setTextColor(Color.rgb(190, 190, 190));
                        }

                        sparseBooleanArray.put(getItemCount(), true);
                        notifyItemChanged(getItemCount());
                        meutotal = meutotal + 1;
                    } else {
                        sparseBooleanArray.put(getItemCount(), false);
                        notifyItemChanged(getItemCount());
                        meutotal = meutotal - 1;
                    }

                    if (!sparseBooleanArray.get(getItemCount())) {
                        if (meutotal < 4) {
                            holder.itemTextView.setTextColor(Color.rgb(236, 0, 140));
                            sparseBooleanArray.put(getItemCount(), true);
                            holder.DICategoriasIcones.setImageResource(R.drawable.sub_categorias_animais);
                            notifyItemChanged(getItemCount());
                            meutotal = meutotal + 1;
                        }
                    } else // if clicked item is already selected
                    {
                        sparseBooleanArray.put(getItemCount(), false);
                        notifyItemChanged(getItemCount());
                        meutotal = meutotal - 1;
                    }
                }
            }
        });
        holder.itemTextView.setText(item.getCategoria());
        holder.DICategoriasIcones.setImageResource(item.getImagem());
    } else if (position <= 2) {
        holder.DICategoriasIcones.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                test = !test; //Switch boolean value
                holder.itemTextView.setFocusable(test);
                if (test == true) {
                    holder.itemTextView.setTextColor(Color.rgb(236, 0, 140));
                    holder.DICategoriasIcones.setImageResource(R.drawable.sub_categorias_arquitetura);
                } else {
                    holder.DICategoriasIcones.setImageResource(R.drawable.categorias_arquitetura);
                    holder.itemTextView.setTextColor(Color.rgb(190, 190, 190));
                }

                if (sparseBooleanArray.equals(2)) {
                    if (meutotal < 4) {
                        test = !test; //Switch boolean value
                        holder.itemTextView.setFocusable(test);
                        if (test == true) {
                            holder.itemTextView.setTextColor(Color.rgb(236, 0, 140));
                            holder.DICategoriasIcones.setImageResource(R.drawable.sub_categorias_arquitetura);
                        } else {
                            holder.DICategoriasIcones.setImageResource(R.drawable.categorias_arquitetura);
                            holder.itemTextView.setTextColor(Color.rgb(190, 190, 190));
                        }

                        sparseBooleanArray.put(getItemCount(), true);
                        notifyItemChanged(getItemCount());
                        meutotal = meutotal + 1;
                    } else {
                        sparseBooleanArray.put(getItemCount(), false);
                        notifyItemChanged(getItemCount());
                        meutotal = meutotal - 1;
                    }

                    if (!sparseBooleanArray.get(getItemCount())) {
                        if (meutotal < 4) {
                            holder.itemTextView.setTextColor(Color.rgb(236, 0, 140));
                            sparseBooleanArray.put(getItemCount(), true);
                            holder.DICategoriasIcones.setImageResource(R.drawable.sub_categorias_arquitetura);
                            notifyItemChanged(getItemCount());
                            meutotal = meutotal + 1;
                        }
                    } else // if clicked item is already selected
                    {
                        sparseBooleanArray.put(getItemCount(), false);
                        notifyItemChanged(getItemCount());
                        meutotal = meutotal - 1;
                    }
                }
            }
        });
        holder.itemTextView.setText(item.getCategoria());
        holder.DICategoriasIcones.setImageResource(item.getImagem());
    } else if (position <= 3) {
        holder.DICategoriasIcones.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                test = !test; //Switch boolean value
                holder.itemTextView.setFocusable(test);
                if (test == true) {
                    holder.itemTextView.setTextColor(Color.rgb(236, 0, 140));
                    holder.DICategoriasIcones.setImageResource(R.drawable.sub_categorias_astrologia);
                } else {
                    holder.DICategoriasIcones.setImageResource(R.drawable.categorias_astrologia);
                    holder.itemTextView.setTextColor(Color.rgb(190, 190, 190));
                }

                if (sparseBooleanArray.valueAt(0)) {
                    if (meutotal < 4) {
                        test = !test; //Switch boolean value
                        holder.itemTextView.setFocusable(test);
                        if (test == true) {
                            holder.itemTextView.setTextColor(Color.rgb(236, 0, 140));
                            holder.DICategoriasIcones.setImageResource(R.drawable.sub_categorias_astrologia);
                        } else {
                            holder.DICategoriasIcones.setImageResource(R.drawable.categorias_astrologia);
                            holder.itemTextView.setTextColor(Color.rgb(190, 190, 190));
                        }

                        sparseBooleanArray.put(getItemCount(), true);
                        notifyItemChanged(getItemCount());
                        meutotal = meutotal + 1;
                    } else {
                        sparseBooleanArray.put(getItemCount(), false);
                        notifyItemChanged(getItemCount());
                        meutotal = meutotal - 1;
                    }

                    if (!sparseBooleanArray.get(getItemCount())) {
                        if (meutotal < 4) {
                            holder.itemTextView.setTextColor(Color.rgb(236, 0, 140));
                            sparseBooleanArray.put(getItemCount(), true);
                            holder.DICategoriasIcones.setImageResource(R.drawable.sub_categorias_astrologia);
                            notifyItemChanged(getItemCount());
                            meutotal = meutotal + 1;
                        }
                    } else // if clicked item is already selected
                    {
                        sparseBooleanArray.put(getItemCount(), false);
                        notifyItemChanged(getItemCount());
                        meutotal = meutotal - 1;
                    }
                }
            }
        });
        holder.itemTextView.setText(item.getCategoria());
        holder.DICategoriasIcones.setImageResource(item.getImagem());
    } else if (position <= 4) {
        test = !test; //Switch boolean value
        holder.itemTextView.setFocusable(test);

        if (sparseBooleanArray.equals(4)) {
            if (meutotal < 4) {
                test = !test; //Switch boolean value
                holder.itemTextView.setFocusable(test);
                if (test == true) {
                    holder.itemTextView.setTextColor(Color.rgb(236, 0, 140));
                    holder.DICategoriasIcones.setImageResource(R.drawable.sub_categorias_atualidades);
                } else {
                    holder.DICategoriasIcones.setImageResource(R.drawable.categorias_atualidades);
                    holder.itemTextView.setTextColor(Color.rgb(190, 190, 190));
                }

                sparseBooleanArray.put(getItemCount(), true);
                notifyItemChanged(getItemCount());
                meutotal = meutotal + 1;
            } else {
                sparseBooleanArray.put(getItemCount(), false);
                notifyItemChanged(getItemCount());
                meutotal = meutotal - 1;
            }

            if (!sparseBooleanArray.get(getItemCount())) {
                if (meutotal < 4) {
                    holder.itemTextView.setTextColor(Color.rgb(236, 0, 140));
                    sparseBooleanArray.put(getItemCount(), true);
                    holder.DICategoriasIcones.setImageResource(R.drawable.sub_categorias_atualidades);
                    notifyItemChanged(getItemCount());
                    meutotal = meutotal + 1;
                }
            } else // if clicked item is already selected
            {
                sparseBooleanArray.put(getItemCount(), false);
                notifyItemChanged(getItemCount());
                meutotal = meutotal - 1;
            }
        }
        holder.itemTextView.setText(item.getCategoria());
        holder.DICategoriasIcones.setImageResource(item.getImagem());
    } else if (position <= 5) {
        test = !test; //Switch boolean value
        holder.itemTextView.setFocusable(test);

        if (sparseBooleanArray.equals(5)) {
            if (meutotal < 4) {
                test = !test; //Switch boolean value
                holder.itemTextView.setFocusable(test);
                if (test == true) {
                    holder.itemTextView.setTextColor(Color.rgb(236, 0, 140));
                    holder.DICategoriasIcones.setImageResource(R.drawable.sub_categorias_autoconhecimento);
                } else {
                    holder.DICategoriasIcones.setImageResource(R.drawable.categorias_autoconhecimento);
                    holder.itemTextView.setTextColor(Color.rgb(190, 190, 190));
                }

                sparseBooleanArray.put(getItemCount(), true);
                notifyItemChanged(getItemCount());
                meutotal = meutotal + 1;
            } else {
                sparseBooleanArray.put(getItemCount(), false);
                notifyItemChanged(getItemCount());
                meutotal = meutotal - 1;
            }

            if (!sparseBooleanArray.get(getItemCount())) {
                if (meutotal < 4) {
                    holder.itemTextView.setTextColor(Color.rgb(236, 0, 140));
                    sparseBooleanArray.put(getItemCount(), true);
                    holder.DICategoriasIcones.setImageResource(R.drawable.sub_categorias_autoconhecimento);
                    notifyItemChanged(getItemCount());
                    meutotal = meutotal + 1;
                }
            } else // if clicked item is already selected
            {
                sparseBooleanArray.put(getItemCount(), false);
                notifyItemChanged(getItemCount());
                meutotal = meutotal - 1;
            }
        }
        holder.itemTextView.setText(item.getCategoria());
        holder.DICategoriasIcones.setImageResource(item.getImagem());
    } else if (position <= 6) {
        holder.itemTextView.setText(item.getCategoria());
        holder.itemTextView.setBackgroundColor(Color.TRANSPARENT);
        holder.DICategoriasIcones.setImageResource(item.getImagem());
        holder.itemTextView.setTextColor(Color.rgb(190, 190, 190));
        holder.DICategoriasIcones.setEnabled(true);
        holder.itemTextView.setEnabled(true);
    } else if (position <= 7) {
        holder.itemTextView.setText(item.getCategoria());
        holder.itemTextView.setBackgroundColor(Color.TRANSPARENT);
        holder.DICategoriasIcones.setImageResource(item.getImagem());
        holder.itemTextView.setTextColor(Color.rgb(190, 190, 190));
        holder.DICategoriasIcones.setEnabled(true);
        holder.itemTextView.setEnabled(true);
    } else {
        holder.itemTextView.setText(item.getCategoria());
        if (sparseBooleanArray.get(position)) {
            holder.itemTextView.setBackgroundColor(Color.TRANSPARENT);
            holder.itemTextView.setTextColor(Color.rgb(190, 190, 190));
        } else {
            holder.itemTextView.setBackgroundColor(Color.TRANSPARENT);
            holder.itemTextView.setTextColor(Color.rgb(190, 190, 190));
        }
    }

}

@Override
public int getItemCount() {
    if (TagsDaMarca != null) {
        return TagsDaMarca.size();
    } else if (SubServicoTagsDaMarca != null) {
        return SubServicoTagsDaMarca.size();
    } else if (DICategorias != null) {
        return DICategorias.size();
    }
    return 0;
}


/******** View Holder Class*/
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView itemTextView;
    ImageView DICategoriasIcones;
    public TextView alert;

    public ViewHolder(View itemView) {
        super(itemView);
        itemTextView = itemView.findViewById(R.id.itemTextView);
        itemTextView.setOnClickListener(this);

        DICategoriasIcones = itemView.findViewById(R.id.DICategoriasIcones);
        DICategoriasIcones.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        if (!sparseBooleanArray.get(getAdapterPosition())) {
            if (meutotal < 4) {
                sparseBooleanArray.put(getAdapterPosition(), true);
                notifyItemChanged(getAdapterPosition());
                meutotal = meutotal + 1;
            }
        } else // if clicked item is already selected
        {
            sparseBooleanArray.put(getAdapterPosition(), false);
            notifyItemChanged(getAdapterPosition());
            meutotal = meutotal - 1;
        }
    }
}

public interface OnRecyclerViewItemClickListener {
    public void selectedItemCount(int count);
}
}

我在RecyclerView上已经具有此onClick,但是如何与onBindViewHolder一起使用?

@Override
public void onClick(View view) {
        //meutotal is for loop to limit to select only 4 categories
    if (!sparseBooleanArray.get(getAdapterPosition())) {
        if (meutotal < 4) {
            sparseBooleanArray.put(getAdapterPosition(), true);
            notifyItemChanged(getAdapterPosition());
            meutotal = meutotal + 1;
        }
    } else // if clicked item is already selected
    {
        sparseBooleanArray.put(getAdapterPosition(), false);
        notifyItemChanged(getAdapterPosition());
        meutotal = meutotal - 1;
    }
}
}

1 个答案:

答案 0 :(得分:0)

onBindViewHolder()方法被多次调用,可以使用变量进行更新,
创建一个变量selectedItemPosition = -1; 现在只需像这样在您的onClick()方法中设置变量,

@Override
public void onClick(View view) {
        if(selectedItemPosition != getAdapterPosition()) {
          selectedItemPosition = getAdapterPosition();
          notifyDataSetChanged();
         } else {
           //handle what happens if the user clicks the same item again
         }

        //meutotal is for loop to limit to select only 4 categories
    if (!sparseBooleanArray.get(getAdapterPosition())) {
        if (meutotal < 4) {
            sparseBooleanArray.put(getAdapterPosition(), true);
            notifyItemChanged(getAdapterPosition());
            meutotal = meutotal + 1;
        }
    } else // if clicked item is already selected
    {
        sparseBooleanArray.put(getAdapterPosition(), false);
        notifyItemChanged(getAdapterPosition());
        meutotal = meutotal - 1;
    }
}
}

根据您的需求,使用彩色滤光片比替换图像更好,因为它可以节省应用程序的空间, 在您的onBindViewHolder()方法中,添加以下代码,

if(selectedItemPosition != -1 && selectedItemPosition  == position) {
   viewHolder.imageView.setColorFilter(ContextCompat.getColor(mContext, R.color.yourcolor));
}