Reputation: 835
When I scroll the recyclerView some items appear in positions where they shouldn't.
Where I should have only "First" or only "Second" suddenly I have both "First Second" in a view.
EDIT : most probably it reuses some views but how am I going to check if this is correct?
My code is :
@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);
}
And finally my ViewHolder is :
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);
}
}
Upvotes: 1
Views: 65
Reputation: 3259
The problem is in the way you define if statements without else clause. What happens is when the view holder gets recycled, views such as tvSplash1, tvSplash2 and etc. still have values assigned from the view that got off the screen. That's why you need to reset these items as follows:
@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("");
}
}
}
Upvotes: 1