gpt4 book ai didi

android - RecyclerView OnBindViewHolder 意外

转载 作者:行者123 更新时间:2023-11-29 00:09:47 25 4
gpt4 key购买 nike

onBindViewHolder 在数据绑定(bind)到 View 后的第一个位置和最后一个位置意外调用。这造成了问题,因为当我尝试向列表中添加项目时,最后一项的 ImageViews(相似和不同)正在与列表中的第一项交换/回收。我怎样才能防止这种情况发生?

回收器适配器代码:

public class RecyclerAdapterForOpinion extends RecyclerView.Adapter<RecyclerAdapterForOpinion.OpinionViewHolder> {

private LayoutInflater inflater;
private List<ParseObject> opinionList;
private String currentUsername = Application.currentUser.getString("Name");

public RecyclerAdapterForOpinion(Context context, List<ParseObject> opinionList) {
inflater = LayoutInflater.from(context);
this.opinionList = opinionList;
}

@Override
public OpinionViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

Log.d("RecyclerAdapter", "OnCreateViewHolder called");
View itemView = inflater.inflate(R.layout.item_view_poll, viewGroup, false);
return new OpinionViewHolder(itemView);
}

@Override
public void onBindViewHolder(final OpinionViewHolder opinionViewHolder, final int position) {

Log.d("RecyclerAdapter", "OnBindViewHolder called for position: " + position);
ParseObject current = opinionList.get(position);
opinionViewHolder.entry.setText(current.getString("entry"));
opinionViewHolder.rationale.setText(current.getString("rationale"));
opinionViewHolder.priority.setText("" + current.getInt("priority"));

List<String> userLikes = (List<String>) current.get("userLike");
List<String> userUnlikes = (List<String>) current.get("userUnlike");

if (userLikes != null) {
if(userLikes.size() != 0){
if (userLikes.contains(currentUsername)) {
opinionViewHolder.like.setChecked(true);
}
}
}
if (userUnlikes != null) {
if(userUnlikes.size() != 0) {
if (userUnlikes.contains(currentUsername)) {
opinionViewHolder.unlike.setChecked(true);
}
}
}
}

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

public void addItem(ParseObject opinion) {
opinionList.add(opinion);
notifyDataSetChanged();
}

public class OpinionViewHolder extends RecyclerView.ViewHolder {

TextView entry;
TextView rationale;
TextView priority;
CheckBox like, unlike;
ImageButton trash;

public OpinionViewHolder(View itemView) {
super(itemView);
entry = (TextView) itemView.findViewById(R.id.opinionEntry);
like = (CheckBox) itemView.findViewById(R.id.opinionLike);
unlike = (CheckBox) itemView.findViewById(R.id.opinionUnlike);
rationale = (TextView) itemView.findViewById(R.id.opinionRationale);
priority = (TextView) itemView.findViewById(R.id.opinionPriority);
trash = (ImageButton) itemView.findViewById(R.id.trash);


like.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ParseObject current = opinionList.get(getAdapterPosition());
CheckBox check = (CheckBox) v;
if (check.isChecked()) {
Application.likeOpinion(current);
} else {
Application.removeLike(current);
}
priority.setText("" + current.getInt("priority"));
}
});

unlike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ParseObject current = opinionList.get(getAdapterPosition());
CheckBox check = (CheckBox) v;
if (check.isChecked()) {
Application.unlikeOpinion(current);
} else {
Application.removeUnlike(current);
}
priority.setText("" + current.getInt("priority"));
}
});
}
}

}

Logcat 输出:

06-30 19:47:01.499    8579-8579/com.nishantjain.dime.dime D/RecyclerAdapter﹕ OnCreateViewHolder called

06-30 19:47:01.519 8579-8579/com.nishantjain.dime.dime D/RecyclerAdapter﹕ OnBindViewHolder called for position: 0

06-30 19:47:01.529 8579-8579/com.nishantjain.dime.dime D/RecyclerAdapter﹕ OnCreateViewHolder called

06-30 19:47:01.539 8579-8579/com.nishantjain.dime.dime D/RecyclerAdapter﹕ OnBindViewHolder called for position: 1

06-30 19:47:01.539 8579-8579/com.nishantjain.dime.dime D/RecyclerAdapter﹕ OnCreateViewHolder called

06-30 19:47:01.549 8579-8579/com.nishantjain.dime.dime D/RecyclerAdapter﹕ OnBindViewHolder called for position: 2

06-30 19:47:01.549 8579-8579/com.nishantjain.dime.dime D/RecyclerAdapter﹕ OnBindViewHolder called for position: 0

06-30 19:47:01.549 8579-8579/com.nishantjain.dime.dime D/RecyclerAdapter﹕ OnBindViewHolder called for position: 2

最佳答案

在适配器中,始终将 else 与每个 if 语句一起使用。特别是在处理 View 时。

if (userLikes != null && userLikes.size() != 0 && userLikes.contains(currentUsername)) {
opinionViewHolder.like.setChecked(true);
}else{
opinionViewHolder.like.setChecked(false);
}

对另一个做同样的事情。希望它能解决你的问题。

关于android - RecyclerView OnBindViewHolder 意外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31151908/

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