RecycleView adapter data show wrong when scrolling too fast

末鹿安然 提交于 2019-11-28 09:19:17

问题


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

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 

}

and this is 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

if (convertView == null) {

this is the link.


回答1:


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.

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.

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 



回答2:


I was found this solution after 3 day...hope it will work for you.

I had the same problem and the only solution I found for this is:

holder.setIsRecyclable(false);

Your recycler will not recycle anymore so the items will be the same when you scroll, and if you want to delete some item do not use notifyitemRemoved(position), use notifyDataSetChanged() instead.



来源:https://stackoverflow.com/questions/35885021/recycleview-adapter-data-show-wrong-when-scrolling-too-fast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!