android - Recyclerview中的行位置错误地改变了背景颜色
问题描述
我有一个recyclerview。我正在传递 Recyclerview 项目的列表。在该列表中,如果该 int == 1,我有一个 int 变量名称“isAlready” ,我正在更改背景颜色。但项目位置在滚动时会发生变化,因此它会更改错误项目的背景颜色。我知道 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();
}
}
用户列表:
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;
}
}
解决方案
在该列表中,如果该 int == 1,我有一个 int 变量名称“isAlready”,我正在更改背景颜色。但是项目位置在滚动时会发生变化,因此它会更改错误项目的背景颜色。我知道 recyclerview 将重用该项目。所以请给出一个解决方案。这是我的代码。
正如您正确指出的那样,这是RecyclerView
' 细胞的回收机制。为了修复它,如果条件为假,您将不得不简单地“重置”背景颜色。例如
if (listOfUserPojs.get(position).isAlready()==1){
holder.card_view.setBackgroundColor(Color.GRAY);
} else {
holder.card_view.setBackgroundColor(Color.YOUR_DEFAULT_COLOR)
}
为了提高可读性,您可以考虑在您ViewHolder
的更新中创建一个方法UI
,然后从onBindViewHolder
推荐阅读
- matlab - “输入参数过多”是什么意思?
- amazon-dynamodb - 从 sqs 读取 python 代码并写入 dynamodb 时出错
- java - Libgdx Box2d 多个 SpriteBatch 和 OrthographicCamera
- c# - 如何在空格和其他非字母数字字符处拆分
- python - GNS3 错误:WaitForConnectionWorker 线程停止并出现错误:无法连接到端口 3090 上的 127.0.0.1:[Errno 111] Conexión rehusada
- sql - 如何对多个表使用联合联接,然后使用 WHERE 过滤它们
- node.js - schema.methods 与 schema.statics
- python - 如何在 Pandas 0.24.2 中将 offsets.MonthEnd 系列转换为 int
- android - 将诸如“5:4”之类的字符串转换为数值的最简单方法是什么
- bash - bash 中的三单引号是什么意思?