当我滚动recyclerView时,某些项目出现在不应该出现的位置。
我应该只有“第一个”或者只有“第二个”突然间我在视图中同时拥有“第一秒”。
编辑:最有可能重用一些观点,但我该如何检查这是否正确?
我的代码是:
@Override
public void onBindViewHolder(MyRecyclerAdapter.ViewHolder holder, int position) {
DiaryDay item = items.get(position);
holder.tvDay.setText(item.getDay());
holder.tvMonth.setText(item.getMonth());
showSplash(holder, item);
if (holder.tvSunBathingMins!=null) {
holder.tvSunBathingMins.setText(String.valueOf(item.getSunBathing()) + " mins sunbathing. ");
}
}
private void showSplash(ViewHolder holder, DiaryDay item) {
if ( item.getSun_1() + item.getSun_2() + item.getSun_3() + item.getSun_4() == 4 ) {
holder.tvSplash2.setText( "All day! Amazing!");
} else {
if (item.getSun_1() != 0) {
holder.tvSplash1.setText("First ");
}
if (item.getSun_2() != 0) {
holder.tvSplash2.setText("Second ");
}
if (item.getSun_3() != 0) {
holder.tvSplash3.setText("Third ");
}
if (item.getSun_4() != 0) {
holder.tvSplash4.setText("Fourth ");
}
}
}
@Override
public MyRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
return new ViewHolder(v);
}
最后我的ViewHolder是:
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvDay;
public TextView tvMonth;
public TextView tvSplash1; public TextView tvSplash2; public TextView tvSplash3; public TextView tvSplash4;
public TextView tvSunBathingMins;
public TextView tvSumMins;
public ViewHolder(View itemView) {
super(itemView);
tvDay = (TextView) itemView.findViewById(R.id.dayID);
tvMonth = (TextView) itemView.findViewById(R.id.monthID);
tvSplash1 = (TextView) itemView.findViewById(R.id.sun1);
tvSplash2 = (TextView) itemView.findViewById(R.id.sun2);
tvSplash3 = (TextView) itemView.findViewById(R.id.sun3);
tvSplash4 = (TextView) itemView.findViewById(R.id.sun4);
tvSunBathingMins = (TextView) itemView.findViewById(R.id.sunbathMinsID);
tvSumMins = (TextView) itemView.findViewById(R.id.sumMinsID);
}
}
答案 0 :(得分:1)
问题在于你定义if语句而没有else子句。当视图持有者被回收时,会发生这样的事情,例如tvSplash1,tvSplash2等视图仍然具有从离开屏幕的视图中分配的值。这就是您需要按如下方式重置这些项目的原因:
@Override
public void onBindViewHolder(MyRecyclerAdapter.ViewHolder holder, int position) {
DiaryDay item = items.get(position);
holder.tvDay.setText(item.getDay());
holder.tvMonth.setText(item.getMonth());
showSplash(holder, item);
if (holder.tvSunBathingMins!=null) {
holder.tvSunBathingMins.setText(String.valueOf(item.getSunBathing()) + " mins sunbathing. ");
} else {
holder.tvSunBathingMins.setText("");
}
}
private void showSplash(ViewHolder holder, DiaryDay item) {
if ( item.getSun_1() + item.getSun_2() + item.getSun_3() + item.getSun_4() == 4 ) {
holder.tvSplash2.setText( "All day! Amazing!");
} else {
if (item.getSun_1() != 0) {
holder.tvSplash1.setText("First ");
} else {
holder.tvSplash1.setText("");
}
if (item.getSun_2() != 0) {
holder.tvSplash2.setText("Second ");
} else {
holder.tvSplash2.setText("");
}
if (item.getSun_3() != 0) {
holder.tvSplash3.setText("Third ");
} else {
holder.tvSplash3.setText("");
}
if (item.getSun_4() != 0) {
holder.tvSplash4.setText("Fourth ");
} else {
holder.tvSplash4.setText("");
}
}
}