'The notifyItemRangeInserted command isn't working

I'm having a problem when call the notifyItemRangeInserted of the adapter. When I call this method, nothing happens, simple as that. I've tried to set some println() in the ViewHolderAdapter, but he isn't called, so I can't view the prints.

I've tried all of the "notify" commands of the adapter, and none of these work. Simply nothing happens.

That's my MainActivity. All the objects and arrays I've tested, all of them are working like a charm. I can't understand why the notify doesn't work.

class MainActivity:AppCompatActivity(){

    //Declarations of the variables
    var pageNumber = 1
    var limitPerPage = 5
    lateinit var product: Product
    var productList = ArrayList<EachProduct>()
    var myAdapter  =ViewHolderAdapter(productList, productList.size)

    override fun onCreate(savedInstanceState:Bundle?){
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)
        recyclerView.layoutManager = LinearLayoutManager(this@MainActivity)
        recyclerView.adapter = myAdapter

The code to add items on the list and notify the ViewHolderAdapter is


    //update the product list
    fun updateProductList(product:Product){
        for(i in 0 until 5 step 1){
            productList.add(product.produtos[i])
        }
        showData(productList,pageNumber*limitPerPage)//then notify
    }

    fun showData(productList:List<EachProduct>,productsListSize:Int){
        myAdapter.notifyItemRangeInserted(0,productList.size)
    }

That's my ViewHolderAdapter class

class ViewHolderAdapter(private var products: List<EachProduct>, private val productsListSize: Int): RecyclerView.Adapter<ViewHolderAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent:ViewGroup,viewType:Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_layout, parent, false)
        returnViewHolder(view)
    }

    override fun getItemCount() = productsListSize

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        holder.productName.text=products[position].nome
        Picasso.get().load(products[position].fabricante.img).into((holder.productLogo))
    }

    class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {

        val productName:TextView=itemView.ProductName

        var productLogo:ImageView=itemView.ProductLogo
    }

}

I expect the ViewHolderAdapter to be called, but this is not occurring. Why is that happens? I can't understand. I'll be very grateful if someone could help me.



Solution 1:[1]

A reason can be that the initial size of the item list you want to show is 0 and the recycler view height is set to wrap content. At the moment, for this case I see 2 options:

  1. Keep wrap content for recycler view and make sure the initial list size > 0.
  2. Set the height of the recycler view to match_parent or a fixed size and notifyItemRangeInserted will work without issues.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Rauca