【问题标题】:Clear last selected position OnClick in Recycler View adapter在 Recycler View 适配器中清除最后选择的位置 OnClick
【发布时间】:2017-05-26 04:09:21
【问题描述】:

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

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

【问题讨论】:

    标签: android android-recyclerview onclicklistener highlight listitem


    【解决方案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);
            }
        });
    }
    

    【讨论】:

    • 滚动列表时您的代码会与数据重叠
    • 操作,你是对的。但在这种情况下,持有人不可回收。不仅在我的代码中,而且在问题中。
    【解决方案2】:

    将您的代码更新为此它将起作用:

    @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,因此当您第一次加载列表时,它将显示初始背景颜色。

    【讨论】:

    • 您是否初始化了 selectedPosition = -1 以及适配器的其他变量?
    • 是的,int selectedPosition = -1
    • 你在哪里定义它?
    • 它是一个全局变量。
    【解决方案3】:

    发生的情况是您的代码仅更改布局从默认设置为选中。您永远不会将布局重置为默认值。看看下面的代码:

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

    @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,在其中将颜色设置为默认情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 2020-04-07
      • 2015-10-18
      • 2017-08-26
      • 1970-01-01
      相关资源
      最近更新 更多