我有一个recyclerview。我正在传递Recyclerview项目的列表。在该列表中,我有一个int变量名称“ isAlready” ,如果该int == 1,我正在更改背景颜色。但是滚动时项目位置正在更改,因此它更改错误项目的背景颜色。我知道recyclerview将 重复使用该物品。因此请提供解决方案。 这是我的代码。
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;
}
}
答案 0 :(得分:2)
在该列表中,如果该int == 1,则我有一个int变量名“ isAlready”, 我正在更改背景颜色。但是项目位置在更改 滚动以便更改错误项目的背景颜色。我知道 recyclerview将重复使用该物品。因此请提供解决方案。这是我的 代码。
正如您正确指出的那样,这就是RecyclerView
单元格的回收机制。为了修复它,如果条件为假,您将只需要简单地“重置”背景色即可。 EG
if (listOfUserPojs.get(position).isAlready()==1){
holder.card_view.setBackgroundColor(Color.GRAY);
} else {
holder.card_view.setBackgroundColor(Color.YOUR_DEFAULT_COLOR)
}
要提高可读性,您可以考虑在ViewHolder
中创建一种方法来更新UI
,然后从onBindViewHolder
调用它
答案 1 :(得分:1)
if (listOfUserPojs.get(position).isAlready()==1){
holder.card_view.setBackgroundColor(Color.GRAY);
}else holder.card_view.setBackgroundColor(0);
}