0

I have a recyclerview. I am Passing List for Recyclerview item.In that List I have one int variable name "isAlready" if that int==1, i am changing background color.but item position is changing while scrolling so that it changing background color for wrong item.i know recyclerview will reuse the item.so please give a solution. Here is my code.

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

Context context;
List<ListOfUserPojo> listOfUserPojs;

private CallBackRequest mCallBack;


public ListOfUserAdapter(Context context,List<ListOfUserPojo> listOfUserPojs,CallBackRequest callBackRequest) {
    this.context=context;
   this.listOfUserPojs=listOfUserPojs;
    mCallBack=callBackRequest;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)   {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_class, parent, false);

    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
     holder.textView.setText(listOfUserPojs.get(position).getProfile().getFullname());
   Log.d("size ",""+listOfUserPojs.size());
    // declare the builder object once.
    TextDrawable.IBuilder builder = TextDrawable.builder()
            .beginConfig()
            .endConfig()
            .round();

    ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT

    // reuse the builder specs to create multiple drawables
    TextDrawable textDrawable = builder.build(listOfUserPojs.get(position).getProfile().getFullname().substring(0,1), generator.getRandomColor());

    holder.imgUser.setImageDrawable(textDrawable);
    Log.d("boolean ",""+listOfUserPojs.get(position).getUserId());
    if (listOfUserPojs.get(position).isAlready()==1){
        holder.card_view.setBackgroundColor(Color.GRAY);
    }

    holder.card_view.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              if (listOfUserPojs.get(position).isAlready()!=1){
                  showDialog(listOfUserPojs.get(position).getProfile().getFullname(),Integer.valueOf(listOfUserPojs.get(position).getUserId()));
              } else {
                  Toast.makeText(context, "You have shown Interest already", Toast.LENGTH_SHORT).show();
              }

          }
      });
}

@Override
public int getItemCount() {
    return  listOfUserPojs.size();
}

@Override
public long getItemId(int position) {
    return super.getItemId(position);
}

public class ViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.textUserName)
    TextView textView;


    @BindView(R.id.card_view)
    LinearLayout card_view;

    @BindView(R.id.img_user)
    ImageView imgUser;


    public ViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);

    }
}


private void showDialog(final String name, final Integer userId) {

    // creating the fullscreen dialog
    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.audio_dialog);

    if (dialog.getWindow() != null) {

        dialog.getWindow().getAttributes().windowAnimations =    R.style.DialogAnimation;
        dialog.getWindow().setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }
    dialog.setCanceledOnTouchOutside(false);
    dialog.setCancelable(true);
    Button btn_yes = dialog.findViewById(R.id.btn_yes);
    Button btn_no = dialog.findViewById(R.id.btn_no);
    TextView name_=dialog.findViewById(R.id.txt_name);

    name_.setText(name);

    btn_yes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           mCallBack.sendRequestCall();
            Intent intent =new Intent(context,ConnectingActivity.class);
            intent.putExtra("userid",userId);
            intent.putExtra("name",name);
            context.startActivity(intent);
            dialog.dismiss();

        }
    });



    btn_no.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();


        }
    });
    dialog.show();
}

public interface CallBackRequest{
    void sendRequestCall();

}
}

ListofuserPojo:

    public class ListOfUserPojo {


@JsonProperty("profile")
private Profile profile;

@JsonProperty("userId")
private String userId;

public Profile getProfile ()
{
    return profile;
}

public void setProfile (Profile profile)
{
    this.profile = profile;
}

public String getUserId ()
{
    return userId;
}

public void setUserId (String userId)
{
    this.userId = userId;
}

@Override
public String toString()
{
    return "ClassPojo [profile = "+profile+", userId = "+userId+"]";
}

public class Profile
{
    @JsonProperty("fullname")
    private String fullname;

    public String getFullname ()
    {
        return fullname;
    }

    public void setFullname (String fullname)
    {
        this.fullname = fullname;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [fullname = "+fullname+"]";
    }
}
@JsonIgnore
private int isAlready ;

public int isAlready() {
    return isAlready;
}

public void setAlready(int already) {
    isAlready = already;
}
}
2

In that List I have one int variable name "isAlready" if that int==1, i am changing background color.but item position is changing while scrolling so that it changing background color for wrong item.i know recyclerview will reuse the item.so please give a solution. Here is my code.

As you correctly stated that's the recycling mechanism of the RecyclerView's cells. In order to fix it you will have to simply 'reset' the background color if the condition is false. EG

 if (listOfUserPojs.get(position).isAlready()==1){
      holder.card_view.setBackgroundColor(Color.GRAY);
 } else {
      holder.card_view.setBackgroundColor(Color.YOUR_DEFAULT_COLOR)
 }

to improve readability you could think about create a method in your ViewHolder that updates the UI, and call it from onBindViewHolder

1
if (listOfUserPojs.get(position).isAlready()==1){
    holder.card_view.setBackgroundColor(Color.GRAY);
}else holder.card_view.setBackgroundColor(0);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.