RecyclerView items duplicate and constantly changing

后端 未结 8 2233
遥遥无期
遥遥无期 2020-12-13 04:14

What\'s Happening:

The list (RecyclerView) is mixing up the data when I scroll.

I.E when I scr

相关标签:
8条回答
  • 2020-12-13 05:02

    done .

    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public int getItemViewType(int position) {
       return position;
    }
    
    0 讨论(0)
  • 2020-12-13 05:02

    Only two things in your RecyclerView adapter

     @Override
    public long getItemId(int position) {
        return position;
    }
    

    and in constructor of adapter

     setHasStableIds(true);
    

    Hope it helps!

    0 讨论(0)
  • 2020-12-13 05:06

    If anybody using kotlin, use like inside RecyclerView.Adapter

    The setHasStableId(true) is to be applied to the adapter of RecylerView

     init {
            setHasStableIds(true);
        }
    

    Override the position with product id

    override fun getItemId(position: Int): Long {
            return myList?.get(position).id
        }
    
    0 讨论(0)
  • 2020-12-13 05:12

    Just add

    override fun getItemViewType(position: Int): Int {
         return position
    }
    
    0 讨论(0)
  • 2020-12-13 05:14

    I had the same problem with a big grid recyclerview with hundreds of numbers that should have appeared sequentially but their order was getting mixed up and repeated.

    // More concise code with Kotlin expressions
    override fun getItemId(position: Int) = position.toLong()
    override fun getItemViewType(position: Int) = position
    

    Added above lines to the adapter.

    0 讨论(0)
  • 2020-12-13 05:14

    In kotlin with clearing the previous items from the LinearLayout list item from the RecyclerView

    override fun onBindViewHolder(view: MyViewHolder, index: Int) {
            view.guideLinearLayout.removeAllViews()
    /* do binding here*/
    }
    
    override fun getItemId(position: Int): Long {
           return position.toLong()
    }
    
    override fun getItemViewType(position: Int): Int {
           return position
    }
    
    0 讨论(0)
提交回复
热议问题