title: My RecyclerView isn't notified appropriately
body: <p>I have a <em>RecyclerView</em> activity and a <em>ViewModel</em> class. The activity calls a method in ViewModel which consumes a web-service, and it also observes a LiveData field. Every time the method gets back an an item from the web-service it sets it to the LiveData and so the observer in the Activity is notified and thus all items enter the RecyclerView. It is guaranteed that this flow works correctly as I can see so in the logs, and in the UI too. </p>
  
  <p>The problem arises when I put a delay with <em>Thread.slepp(500)</em>  in the method where the web-service is consumed.</p>
  
  <p>Instead of putting an item in the RecyclerView then wait for 500millis and then put another, it waits 500 millis * numberOfItems and then draws them all together. </p>
  
  <p>I can assure that there is no problem with the ViewModel and LiveData setup because the log works as intended meaning, it print the title of the item created, waits 500 millis, prints the next one. So the problem lies only with the adapter and how it's notified only after the method call is complete. </p>
  
  <p><strong>My question is how can I notify the adapter every-time the observer is called?</strong></p>
  
  <p>here is my implementation of the three classes:</p>
  
  <p><strong>RecipeList</strong></p>
  
  <pre><code>class RecipeList : LifecycleActivity() {
  
  var recipeList: MutableList&lt;Recipe&gt; = mutableListOf()
  var adapter: RecipeAdapter? = null
  var viewModel: RecipeViewModel? = null
  
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_recipe_list)
  
      val ingredients = intent.getStringExtra("ingredients")
      val term = intent.getStringExtra("term")
  
  
      viewModel = ViewModelProviders.of(this).get(RecipeViewModel::class.java)
  
  
      val url = "http://www.recipepuppy.com/api/?i=${ingredients}onions,garlic&amp;q=${term}"
  
  
      val layoutManager = LinearLayoutManager(this)
      adapter = RecipeAdapter(this, recipeList)
  
      rec_recycler_id.layoutManager = layoutManager
      rec_recycler_id.adapter = adapter
  
  
  
      subscribe()
  
  
      viewModel?.getRecipe(url)
  
  
  }
  
  fun subscribe() {
  
      val observer = Observer&lt;Recipe&gt; { recipe -&gt;
  
  
          if (recipe != null) {
  
              Log.d("mike", "subscribe ${recipe?.title} ")
  
  
              recipeList.add(recipe)
              adapter?.notifyDataSetChanged()
  
  
          }
  
      }
  
      viewModel?.mRecipe?.observe(this, observer)
  
    }
  
  
  }
  </code></pre>
  
  <p><strong>RecipeViewModel</strong></p>
  
  <pre><code>class RecipeViewModel(application: Application): AndroidViewModel(application) {
  
  var recipes: MutableLiveData&lt;MutableList&lt;Recipe&gt;&gt;? =  MutableLiveData&lt;MutableList&lt;Recipe&gt;&gt;()
  var mRecipe: MutableLiveData&lt;Recipe&gt; = MutableLiveData()
  
  
  
  
  
  fun getRecipe(url:String){
  
      val requestQueue = Volley.newRequestQueue(this.getApplication())
  
  
      val recipeRequest = JsonObjectRequest(Request.Method.GET,url,
              Response.Listener {
                  response: JSONObject -&gt;
                  try {
  
                      val results = response.getJSONArray("results")
  
                      for( i in 0..results.length()-1){
                          var recipeObj = results.getJSONObject(i)
  
                          var title = recipeObj.getString("title")
                          var link = recipeObj.getString("href")
                          var thumbnail = recipeObj.getString("thumbnail")
                          var ingredients = recipeObj.getString("ingredients")
  
                          var recipe = Recipe(title,ingredients,thumbnail,link)
  
  
                          mRecipe.value = recipe
                          Log.d("mike",title)
  
                          Thread.sleep(200)
  
                      }
  
  
                  }catch (e: JSONException){
                      e.printStackTrace()
                  }
              },
              Response.ErrorListener {
                  error: VolleyError? -&gt;
                  try{
                      Log.d("error",error.toString())
  
                  }catch (e: JSONException){
                      e.printStackTrace()
                  }
              })
  
  
      requestQueue?.add(recipeRequest)
  
  }
  </code></pre>
  
  <p>and the <strong>RecipeAdapter</strong></p>
  
  <pre><code>class RecipeAdapter(val context: Context, var recipes: MutableList&lt;Recipe&gt;) : RecyclerView.Adapter&lt;RecipeAdapter.ViewHolder&gt;() {
  
  override fun getItemCount(): Int = recipes.size
  
  
  override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
  
      val view = LayoutInflater.from(context).inflate(R.layout.recipe_rec_row, null)
  
      return ViewHolder(view)
  
  
  }
  
  override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
      holder?.bindViews(recipes[position])
  }
  
  
  inner class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
  
      fun bindViews(recipe: Recipe) {
  
          itemView.textView7.text = recipe.title
          itemView.textView9.text = recipe.ingredients
          itemView.button6.setOnClickListener() {
  
              if(!recipe.link.trim().isEmpty())
                  context.startActivity&lt;ShowLinkAct&gt;("url" to recipe.link)
              else
                  context.toast("No link available")
  
          }
  
          if (!recipe.thumbnail.isEmpty()) {
  
              Picasso.with(context)
                      .load(recipe.thumbnail)
                      .placeholder(android.R.drawable.ic_menu_report_image)
                      .error(android.R.drawable.ic_menu_report_image)
                      .into(itemView.imageView)
  
          } else {
              Picasso.with(context).load(android.R.drawable.ic_menu_report_image).into(itemView.imageView)
          }
  
  
      }
  
  }
  }
  </code></pre>
  
  <p>I am looking forward for your suggestions, Thank you in advance</p>