我有一个自定义的回收视图适配器列出我的项目.在每个项目中,我检查数据库并用颜色绘制一些圆圈.
当我滚动listview非常快时,每个绘制的数据(不是标题和文本)显示错误!
如何在不显示错误数据的情况下管理动态视图创建?!
@Override
public void onBindViewHolder(final ItemViewHolder itemViewHolder, int i) {
itemViewHolder.date.setText(items.get(i).getData()); // set the title
itemViewHolder.relative_layout_tag_place.addView(generateTagImages(items.get(i).getServerId())); // had to generate a Relativelaout with
}
这是importMenuTags():
private RelativeLayout generateTagImages(String serverId) {
List<String> color_list = new ArrayList<>();
RelativeLayout result = new RelativeLayout(context);
List<String> list = db.getCardTags(serverId);
int i = 0;
for (String string : list) {
RelativeLayout rl = new RelativeLayout(context);
color_list.add(get_the_proper_color);
Drawable drawable = context.getResources().getDrawable(R.drawable.color_shape);
drawable.setColorFilter(Color.parseColor(dao.getTagColor(string)), PorterDuff.Mode.SRC_ATOP);
RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.addRule(RelativeLayout.ALIGN_PARENT_START);
lparams.setMargins(i, 0, 0, 0);
lparams.width = 35;
lparams.height = 35;
rl.setLayoutParams(lparams);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
rl.setBackground(drawable);
} else {
rl.setBackgroundDrawable(drawable);
}
result.addView(rl);
i = i + 25;
}
return result;
}
我在简单的自定义适配器中也遇到了同样的问题,它通过移动我的功能位置来解决
if(convertView == null){
这是link.
解决方法:
按在你的代码看,我发现你的相对布局必须显示在滚动一些额外的数据.这是因为回收意见.这里
public void onBindViewHolder(final ItemViewHolder itemViewHolder, int i) {
itemViewHolder.date.setText(items.get(i).getData()); // set the title
itemViewHolder.relative_layout_tag_place.addView(generateTagImages(items.get(i).getServerId())); // had to generate a Relativelaout with
//Problem is here.
假设您在上面的holde.relative_layout中添加了一些子视图,并且此ViewHodler被回收并提供给另一个项目视图,它已经包含了之前添加的所有视图.并且您正在添加新的子视图.希望你能理解你的问题.
解决方案:非常简单.删除onBindViewHolder中所有previsousley添加的视图
public void onBindViewHolder(final ItemViewHolder itemViewHolder, int i) {
itemViewHolder.date.setText(items.get(i).getData()); // set the title
itemViewHolder.relative_layout_tag_place.removeAllViews();
itemViewHolder.relative_layout_tag_place.addView(generateTagImages(items.get(i).getServerId())); // had to generate a Relativelaout with