清除上次选择的位置OnClick在Recycler View适配器中

时间:2017-01-11 11:27:04

标签: android android-recyclerview onclicklistener highlight listitem

我有一个适配器中设置的行业列表。每次单击/选择行业时,名称和背景颜色都会发生变化。但是,当我尝试以下代码时,之前选择的行业不会更改为其默认颜色

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.setIsRecyclable(false);
    holder.txtIndustry.setText(industries.get(position).getIndustryName().trim());
    holder.txtIndustry.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectedPosition = holder.getAdapterPosition();
            // Highlight the background and change the text color.
            if (selectedPosition == position) {
                holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.text_color_blue));
                holder.txtIndustry.setTextColor(Color.WHITE);
            } else {
                holder.itemView.setBackgroundColor(Color.TRANSPARENT);
                holder.txtIndustry.setTextColor(context.getResources().getColor(R.color.text_color_blue));
            }
            notifyItemChanged(selectedPosition);
            callback.selectedIndustryPosition(position);
        }
    });
}

上述问题的解决方案是:

  @Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    industry = industries.get(position);
    holder.setIsRecyclable(false);
    holder.txtIndustry.setText(industry.getIndustryName().trim());

    if (holder.getAdapterPosition() == selectedPosition) {
        // Highlight the background and change the text color.
        holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.text_color_blue));
        holder.txtIndustry.setTextColor(Color.WHITE);
    } else {
        holder.itemView.setBackgroundColor(Color.TRANSPARENT);
        holder.txtIndustry.setTextColor(context.getResources().getColor(R.color.text_color_blue));
    }

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

3 个答案:

答案 0 :(得分:1)

问题是onClick在每个持有者中。我的意思是,每一行都有自己的onClick。如果单击第2行,则只能访问该持有者。

一种解决方案可能是,保留对最后修改过的持有人的引用。

private ViewHolder lastModifiedHoled = null;

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.setIsRecyclable(false);
    holder.txtIndustry.setText(industries.get(position).getIndustryName().trim());
    holder.txtIndustry.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectedPosition = holder.getAdapterPosition();

            // Reset last modified
            if (lastModifiedHoled != null) {
                int lastPosition = lastModifiedHoled.getAdapterPosition();
                lastModifiedHoled.itemView.setBackgroundColor(Color.TRANSPARENT);
                lastModifiedHoled.txtIndustry.setTextColor(context.getResources().getColor(R.color.text_color_blue));
                notifyItemChanged(lastPosition);
            }

            // Highlight the background and change the text color.
            holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.text_color_blue));
            holder.txtIndustry.setTextColor(Color.WHITE);
            notifyItemChanged(selectedPosition);

            lastModifiedHoled = holder;

            callback.selectedIndustryPosition(position);
        }
    });
}

答案 1 :(得分:0)

将您的代码更新为此功能:

@Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.setIsRecyclable(false);
        if (selectedPosition != -1) {
            if (selectedPosition == position) {
                holder.itemView.setBackgroundColor(context.getResources()
                        .getColor(R.color.text_color_blue));
                holder.txtIndustry.setTextColor(Color.WHITE);
            } else {
                holder.itemView.setBackgroundColor(Color.TRANSPARENT);
                holder.txtIndustry.setTextColor(context.getResources()
                        .getColor(R.color.text_color_blue));
            }
        }
        holder.txtIndustry.setText(industries.get(position).getIndustryName()
                .trim());
        holder.txtIndustry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedPosition = holder.getAdapterPosition();
                // Highlight the background and change the text color.
                notifyItemChanged(selectedPosition);
                callback.selectedIndustryPosition(position);
            }
        });
    }

点击时只会在您点击按钮时调用。点击完成后,您将调用适配器代码,而无需编写代码来更改颜色。最初将selectedPosition定义为-1,因此当您第一次加载列表时,它会显示初始背景颜色。

答案 2 :(得分:0)

正在发生的事情是您的代码仅将布局从默认更改为所选内容。您永远不会将布局重置为默认值。看看下面的代码:

在您的侦听器上,您可以重置所有可见项目。

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.setIsRecyclable(false);
    holder.txtIndustry.setText(industries.get(position).getIndustryName().trim());
    holder.txtIndustry.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectedPosition = holder.getAdapterPosition();
            // Highlight the background and change the text color.
            if (selectedPosition == position) {
                holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.text_color_blue));
                holder.txtIndustry.setTextColor(Color.WHITE);
            } else {
                holder.itemView.setBackgroundColor(Color.TRANSPARENT);
                holder.txtIndustry.setTextColor(context.getResources().getColor(R.color.text_color_blue));
            }
            notifyItemChanged(selectedPosition);
            callback.selectedIndustryPosition(position);

            LinearLayoutManager layoutManager = ((LinearLayoutManager) mRecyclerView.getLayoutManager());
            int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
            int lastVisiblePosition = layoutManager.findLastVisibleItemPosition();

            for (int i = firstVisiblePosition; i <= lastVisiblePosition; i++) {
                resetLayoutForPosition(i);
            }
        }
    });
    resetLayoutForPosition(position);
}

然后创建方法resetLayoutForPosition,将颜色设置为默认情况。