【问题标题】:Recycler view item colour change repeating after scrolling滚动后重复循环器视图项目颜色更改
【发布时间】:2022-05-13 20:42:25
【问题描述】:

滚动后重复循环器视图项目颜色更改。

我曾经在 Recyclerview 列表的特定位置更改颜色。当滚动发生时,底部的另一个项目具有相同的变化。它在模式中重复。如何解决?

 holder.recycle_Listing.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            itemListener.connectionClicked(v,position, itemtype);

            holder.mainlayout.setBackgroundColor(Color.parseColor("#e927a4d1"));

        }
    });

【问题讨论】:

  • 请提供更多来自您的适配器的代码,因为您滚动后它会改变颜色,当然是重新创建的视图与您单击的位置相同。
  • 是的,我找到了@AchmadNaufalSyafiq 的解决方案。感谢您的回复。
  • @AndroidSurya 该链接有助于找到答案,但不适合我。谢谢

标签: android android-recyclerview


【解决方案1】:

回收器视图回收 OnBindViewHolder 中的视图。因此,当单击项目时,它会反映在其他一些位置。解决这个问题。创建一个全局 SparseBooleanArray 来存储点击位置。

private final SparseBooleanArray array=new SparseBooleanArray();

然后在最终视图中添加 clickListener 和 onClick 存储被点击项目的位置。

public class ViewHolder extends RecyclerView.ViewHolder {
    public YOURVIEW view;
    public ViewHolder(View v) {
        super(v);
        view = (YOURVIEW) v.findViewById(R.id.YOURVIEWID);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                array.put(getAdapterPosition(),true);
                notifyDataSetChanged();
            }
        });
    }
}

在 OnBindViewHolder 里面,

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    if(array.get(position)){
        holder.mainlayout.setBackgroundColor(Color.parseColor("#e927a4d1"));
    }else{
        holder.mainlayout.setBackgroundColor(UNSELECTEDCOLOR);
    }
}

【讨论】:

  • 可能很多项目都可以点击,所以只有mItemSelected 是不够的。
  • 然后使用sparseBoolean Array。检查更新的代码。
【解决方案2】:

这是由于recycler view的viewholder被回收。尝试这个 它对我有用。

public ListViewHolder(View itemView) {
        super(itemView);

        this.setIsRecyclable(false);


    }

【讨论】:

  • 我认为这不是一个好主意,因为回收站视图用于缓存。
  • 用这个代码禁用了回收站的目的?。这不是一种可接受的方法。
【解决方案3】:

我想你可以在void onBindViewHolder(VH holder, int position);中设置你的背景颜色比如

List<Integer> selectedPosition = new ArrayList(yourDataSize);
void onBindViewHolder(VH holder, int position){

     if(selectedPosition.get(position) == 1){
       holder.mainlayout.setBackgroundColor(Color.parseColor("#e927a4d1"));
     }else {
       holder.mainlayout.setBackgroundColor(normalColor);
     }

     //when the item clicked 
     selectedPosition.add(position,1);

}

【讨论】:

  • 什么是Data类?
  • 找到解决方案并在此处发布。谢谢
【解决方案4】:

该函数返回相对位置而不是绝对位置,因此当屏幕滚动时,位置被替换为新值。使用列表中的位置来获得所需的结果。

【讨论】:

    【解决方案5】:

    尝试在您的适配器类中实现此方法,可能会解决您的问题

    @Override
        public void onViewRecycled(ViewHolderProduct holder) {
            super.onViewRecycled(holder);
            holder.mainlayout.removeAllViews();
    } 
    

    【讨论】:

      【解决方案6】:

      刚刚将每个项目键保存在一个数组中,并且该选定的数组也通过了我的适配器类。在这种格式下,即使是简单的颜色变化也能正常工作。这里的代码是根据我的要求更改的。

      @Override
          public void onBindViewHolder(final ICConversationHomeAddConnectionsAdapter.ViewHolder holder, final int position) {
      
              JsonObject object = dataArray.get(position).getAsJsonObject();
              if(selectedArray.contains(object.get("userkey").getAsString()))
              {
                  GradientDrawable borCol = new GradientDrawable();
                  borCol.setCornerRadius(7);
                  borCol.setColor(Color.parseColor("#ffffff"));
                  borCol.setStroke(2, Color.parseColor("#60B9E1"));
                  holder.recycle_Listing.setBackgroundDrawable(borCol);
                 //holder.mainlayout.setBackgroundColor(Color.parseColor("#e927a4d1"));
              }
              else
              {
                  GradientDrawable borCol = new GradientDrawable();
                  borCol.setCornerRadius(7);
                  borCol.setColor(Color.parseColor("#ffffff"));
                  borCol.setStroke(1, Color.parseColor("#e0e0e0"));
                  holder.recycle_Listing.setBackgroundDrawable(borCol);
                //holder.mainlayout.setBackgroundColor(Color.parseColor("#f1f1f1"));
              }
      
      
                  holder.profileName.setText(object.get("name").getAsString());
      
              holder.recycle_Listing.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
      
      
                      holder.mainlayout.setSelected(!holder.mainlayout.isSelected());
      
                      if (holder.mainlayout.isSelected()) {
                          GradientDrawable borCol = new GradientDrawable();
                          borCol.setCornerRadius(7);
                          borCol.setColor(Color.parseColor("#ffffff"));
                          borCol.setStroke(2, Color.parseColor("#60B9E1"));
                          holder.recycle_Listing.setBackgroundDrawable(borCol);
                        //  holder.mainlayout.setBackgroundColor(Color.parseColor("#11B5DA"));
                      } else {
                          GradientDrawable borCol = new GradientDrawable();
                          borCol.setCornerRadius(7);
                          borCol.setColor(Color.parseColor("#ffffff"));
                          borCol.setStroke(1, Color.parseColor("#e0e0e0"));
                          holder.recycle_Listing.setBackgroundDrawable(borCol);
                         // holder.mainlayout.setBackgroundColor(Color.parseColor("#f1f1f1"));
                      }
      
      
      
                      itemListener.connectionClicked(v,position, itemtype);
      
      
                     //holder.mainlayout.setBackgroundColor(Color.parseColor("#11B5DA"));
                    //holder.mainlayout.setBackgroundColor(Color.parseColor("#f1f1f1"));
                  }
              });
      
      
      
          }
      

      此代码工作正常,回收站中没有重复的颜色变化。如果有任何疑问,请随时通过 cmets 或聊天询问

      【讨论】:

        【解决方案7】:

        Kotlin 中的 Recyclerview 和房间数据库。

        在活动中://发送位置给适配器

        val putPosition = 5   // what you want to be position
        Handler(Looper.getMainLooper()).postDelayed({
               (Recyclerview.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(putPosition-1,0)
        }, 1500)
        //
        //
        // out of onCreate (for companion)
        fun getVersePosition() = putPosition
        

        在适配器中://从 Activity 中获取位置

        override fun onBindViewHolder(holder: Adapter.Holder, position: Int) {
            val roomData = listData[position]
            holder.setRoomData(roomData)
            val activity : MainActivity.Companion = MainActivity
            val getPosition : Int? = activity.companionMainActivity?.getVersePosition()
            if (position == getPosition-1){
                    holder.itemView.setBackgroundColor(Color.parseColor("#AFB42B"))
                } else {
                    holder.itemView.setBackgroundColor(Color.TRANSPARENT)
                }
        }
        

        SparseBooleanArray() 不需要 而且 bind 也不需要。

        但这很重要

        holder.itemView.setBackgroundColor(Color.TRANSPARENT)
        

        如果省略,会出现重复。

        【讨论】:

          猜你喜欢
          • 2019-08-07
          • 1970-01-01
          • 1970-01-01
          • 2015-11-14
          • 1970-01-01
          • 1970-01-01
          • 2021-09-03
          • 1970-01-01
          • 2014-07-05
          相关资源
          最近更新 更多