滚动后重复查看回收者视图项目的颜色变化 [英] Recycler view item colour change repeating after scrolling

查看:66
本文介绍了滚动后重复查看回收者视图项目的颜色变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

滚动后重复显示回收者视图项目的颜色.

Recycler view item color change repeating after scrolling.

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

I used to change color at a particular position of the Recyclerview list. When scrolling occurs another item at the bottom has the same change. And it is repeating in pattern. How to resolve this?

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

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

        }
    });

推荐答案

只需将每个项键保存在一个数组中,所选的数组也将通过我的Adapter类传递.即使简单的颜色更改,在这种格式下也可以正常工作.这里的代码根据我的要求进行了更改.

Just saved every item keys in an array and that selected array also passed through my Adapter class. Even simple colour change works fine in this format. Here the code is changed as per my the requirement.

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



    }

此代码可以正常工作,并且在回收站中无需重复更改颜色.如果有任何疑问,可以通过评论或聊天询问

This code works fine with no repeated colour change in recycler. If any queries feel free to ask via comments or chat

这篇关于滚动后重复查看回收者视图项目的颜色变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆