RecyclerView OnBindViewHolder意外

时间:2015-07-01 03:06:13

标签: android android-recyclerview recycler-adapter

意外地在第一个位置调用

onBindViewHolder,然后在数据绑定到视图后调用最后一个位置。这会产生问题,因为当我尝试向列表添加项目时,最后一项的ImageViews(与之不同)与列表中的第一项交换/回收。我怎样才能防止这种情况发生?

Recycler Adapter Code:

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

1 个答案:

答案 0 :(得分:2)

在适配器中,始终对else个语句使用if。特别是在处理视图时。

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

对其他人也一样。希望它能解决你的问题。