滚动速度过快时RecycleView适配器的数据显示错误 [英] RecycleView adapter data show wrong when scrolling too fast

查看:147
本文介绍了滚动速度过快时RecycleView适配器的数据显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列出我的项目自定义视图回收适配器。在每一个项目我检查数据库,并绘制一些圆圈的颜色。

I have a custom Recycle View adapter that list my items. in each Item I check database and draw some circles with colors.

在我的ListView滚动速度非常快每drawed数据(没有标题和文本)显示是非!
我怎么能没有数据显示错误管理动态视图创建?!

when I scroll listview very fast every drawed data ( not title and texts) show wrongs! how I can manage dynamic View creation without showing wrong data?!

 @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;
}

我也有过,它的移动我的函数的地方出来的。

I also had the same problem in simple custom adapter that it's solved by moving my function place out of

如果(convertView == NULL){

这是链接

推荐答案

根据您的code看到,我发现你的相对布局必须显示在滚动一些额外的数据。而且那是因为意见回收。在这里,

As per seeing in your code, I found your relative layout must be showing some extra data while scrolling. And thats because of recycling of views. Here

   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和recyclerd提供给另一个项目视图,它已经把所有$ P $它pviously补充意见。并要添加与他们新的子视图。希望你理解你的问题。

Suppose you added some child views in above holde.relative_layout , and this ViewHodler is recyclerd and provided to another item view, It already have all previously added views in it. and you are adding new child view with them. Hope you understand your problem.

解决方案:很容易的。删除所有previsousley在onBindViewHolder添加视图

Solution: Very easy one. remove all previsousley added view in onBindViewHolder

   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 

这篇关于滚动速度过快时RecycleView适配器的数据显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆