gpt4 book ai didi

android - 当我向下滚动时,RecyclerView 检查错误的项目

转载 作者:太空宇宙 更新时间:2023-11-03 13:43:55 25 4
gpt4 key购买 nike

我决定使用 RecyclerView 来显示我的数据列表。每个项目/行中有一个复选框、2 个 TextView 和一个 ImageView。我已经创建了我的适配器来处理所有事情,并将选中的项目添加到我的最终列表中。

问题是,当我检查一些项目并向下滚动时,一些项目将显示为已选中,而当我向上滚动时,我之前检查过的项目将显示为未选中。 Images 和 Textviews 没问题,但是上下滚动时复选框会乱七八糟。

这是我的适配器:

public class TeamAdapter extends RecyclerView.Adapter<TeamAdapter.MyViewHolder> {

private Context mContext;
private List<UserProject> UserList;
private UserProject user;


public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView name, description;
public CircleImageView profile_image;
public CheckBox check;

public MyViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.name);
description = (TextView) view.findViewById(R.id.description);
profile_image = (CircleImageView) view.findViewById(R.id.profile_image);
check = (CheckBox) view.findViewById(R.id.check);
}
@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClickListener(v, getAdapterPosition());
}
}
}


public TeamAdapter(Context mContext, List<UserProject> userList) {
this.mContext = mContext;
this.UserList = userList;

}
@Override
public TeamAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.adapter_teams, parent, false);

return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final TeamAdapter.MyViewHolder holder, final int position) {

user = UserList.get(position);

holder.name.setText(user.getName());
holder.description.setText(user.getCompany());

if(user.getImageURL() != null && !user.getImageURL().isEmpty()) {
Picasso.with(mContext).load(user.getImageURL()).into(holder.profile_image);
}

holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
ActivityTeams.newList.add(UserList.get(position));
}else {
ActivityTeams.newList.remove(UserList.get(position));
}
}
});

}


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

private TeamAdapter.onRecyclerViewItemClickListener mItemClickListener;

public void setOnItemClickListener(TeamAdapter.onRecyclerViewItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}

public interface onRecyclerViewItemClickListener {
void onItemClickListener(View view, int position);
}
}

最佳答案

正如在关于重复答案的评论中,您的持有人保留了复选框 View 。您只需根据支持数据更新复选框状态。

onBindViewHolder 中分配检查监听器之前添加:

holder.check.setOnCheckedChangeListener(null);    
holder.check.setChecked(ActivityTeams.newList.contains(UserList.get(position));
holder.check.setOnCheckedChangeListener(new ... "you know what to do here"

基于此处的 CompoundButton 源代码 https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/CompoundButton.java 设置检查:

/**
* <p>Changes the checked state of this button.</p>
*
* @param checked true to check the button, false to uncheck it
*/
@Override
public void setChecked(boolean checked) {
if (mChecked != checked) {
mCheckedFromResource = false;
mChecked = checked;
refreshDrawableState();
notifyViewAccessibilityStateChangedIfNeeded(
AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);

// Avoid infinite recursions if setChecked() is called from a listener
if (mBroadcasting) {
return;
}

mBroadcasting = true;
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
}
if (mOnCheckedChangeWidgetListener != null) {
mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
}
final AutofillManager afm = mContext.getSystemService(AutofillManager.class);
if (afm != null) {
afm.notifyValueChanged(this);
}

mBroadcasting = false;
}
}

如您所见,调用 setChecked 还会调用当前附加的监听器。

关于android - 当我向下滚动时,RecyclerView 检查错误的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46227549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com