title: How to add items to desired ViewHolder position when using multiple viewtypes in RecyclerView?
body: <p><a href="https://i.stack.imgur.com/l99RZ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l99RZ.jpg" alt="enter image description here" /></a></p>
  <p>I drew what I'm trying to implement.</p>
  <p>To explain the function, it is a <code>workout log app</code>.</p>
  <p>Image means that both <code>RoutineItem</code> and <code>DetailItem</code> can be <code>added and deleted</code> by <code>button</code>.</p>
  <p><em>(I didn't include a picture of a button that adds a <code>RoutineItem</code> in the image!)</em></p>
  <p>I made a function that adds both <code>RoutineItem and DetailItem</code>, but I couldn't create a function that adds <code>DetailItem</code> to each <code>RoutineItem</code> last.</p>
  <p>In other words, when the button of each <code>RoutineItem</code> is pressed, a <code>DetailItem</code> should be added to the end of each <code>RoutineItem</code>, but my current implementation is that any <code>RoutineItem</code> button is always added to the end of the <code>List</code> when the button is pressed.</p>
  <p>I'm not sure how to figure out the end of each <code>RoutineItem</code> because I'm implementing it using <code>Multiple ViewHolder</code>.</p>
  <p>How do I know the last <code>DetailItem</code> listed after each <code>RoutineItem</code>?</p>
  <p><em><strong>RoutineItem.kt</strong></em></p>
  <pre><code>sealed class RoutineItem() {
      data class RoutineModel(
          val workout: String, // excercise
          val unit: String, // unit (kg or lbs)
          var routineDetail: List&lt;DetailModel&gt; = listOf()
      ) : RoutineItem()
  
      data class DetailModel(
          val set: String,
          val reps: String = &quot;1&quot;,
          val weight: String
      ) : RoutineItem()
  }
  </code></pre>
  <p><em><strong>ViewHolder.kt</strong></em></p>
  <pre><code>sealed class RoutineItemViewHolder(binding: ViewBinding) : RecyclerView.ViewHolder(binding.root) {
      class RoutineViewHolder(
          private val binding: ItemRoutineBinding,
          private val addDetailClicked: (Int) -&gt; Unit,
          val deleteDetailClicked: (Int) -&gt; Unit
      )
          : RoutineItemViewHolder(binding) {
          init {
              binding.add.setOnClickListener {
                  addDetailClicked(adapterPosition)
              }
          }
          fun bind(item : RoutineItem.RoutineModel) {
              binding.workout.text = item.workout
          }
      }
  
      class RoutineDetailViewHolder(private val binding: ItemRoutineDetailBinding)
          : RoutineItemViewHolder(binding) {
          fun bind() {
              // EMPTY
          }
      }
  }
  </code></pre>
  <p><em><strong>Adapter</strong></em></p>
  <pre><code>class RoutineItemAdapter(
      private val addDetailClicked: (Int) -&gt; Unit,
      private val deleteDetailClicked: (Int) -&gt; Unit) :
      ListAdapter&lt;RoutineItem, RoutineItemViewHolder&gt;(RoutineDiffCallback2()) {
  
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RoutineItemViewHolder {
          return when(viewType) {
              R.layout.item_routine -&gt; RoutineItemViewHolder.RoutineViewHolder(
                  ItemRoutineBinding.inflate(
                      LayoutInflater.from(parent.context),
                      parent,
                      false
                  ),
                  addDetailClicked,
                  deleteDetailClicked
              )
              R.layout.item_routine_detail -&gt; RoutineItemViewHolder.RoutineDetailViewHolder(
                  ItemRoutineDetailBinding.inflate(
                      LayoutInflater.from(parent.context),
                      parent,
                      false
                  )
              )
              else -&gt; throw IllegalArgumentException(&quot;Invalid ViewType Provided&quot;)
          }
      }
  
      override fun onBindViewHolder(holder: RoutineItemViewHolder, position: Int) {
          when(holder) {
  
              is RoutineItemViewHolder.RoutineViewHolder -&gt; holder.bind(currentList[position] as RoutineItem.RoutineModel)
              is RoutineItemViewHolder.RoutineDetailViewHolder -&gt; holder.bind()
          }
      }
  
      override fun getItemCount(): Int = currentList.size // footer 때문에 +1
  
      override fun getItemViewType(position: Int): Int {
          return when(currentList[position]) {
              is RoutineItem.RoutineModel -&gt; R.layout.item_routine
              is RoutineItem.DetailModel -&gt; R.layout.item_routine_detail
              else -&gt; throw IllegalArgumentException(&quot;Invalid ViewType Provided&quot;)
          }
      }
  }
  </code></pre>
  <p><em><strong>ViewModel</strong></em></p>
  <pre><code>class WriteRoutineViewModel : ViewModel() {
      private var _items: MutableLiveData&lt;List&lt;RoutineItem&gt;&gt; = MutableLiveData(listOf())
      private val routines = mutableListOf&lt;RoutineItem&gt;()
  
      val items: LiveData&lt;List&lt;RoutineItem&gt;&gt; = _items
      
      fun addRoutine(workout: String) {
          routines.add(RoutineItem.RoutineModel(workout, &quot;TEST&quot;))
          routines.add(RoutineItem.DetailModel(&quot;1&quot;,&quot;3&quot;,&quot;3&quot;))
          _items.postValue(routines)
  
      }
  
      fun addDetail(pos: Int) {
          routines.add(RoutineItem.DetailModel(&quot;1&quot;,&quot;3&quot;,&quot;3&quot;))
          _items.postValue(routines)
      }
  }
  </code></pre>