title: How to add a header to a PagedList using Android Paging Library
body: <p>I have tried adding a header the normal way (different view type, incrementing the data count by 1 etc.), but the paged list scroll bar jumps to the bottom of the list.</p>
  
  <p>I have since come across <a href="https://github.com/googlesamples/android-architecture-components/issues/375" rel="noreferrer">the AsyncPagedListDiffer</a> and tried to implement it, but now all my items except the header have disappeared. Can anyone see what I am doing wrong?</p>
  
  <pre><code>    class MerchantHistoryAdapter : PagedListAdapter&lt;MerchantHistoryResponse.Transaction, BaseViewHolder&gt;(MerchantHistoryDiffUtilCallback()) {
  
      lateinit var listener: HistoryItemListAdapterListener
      lateinit var headerListener: HistoryItemHeaderListAdapterListener
  
      lateinit var networkStateListener: NetworkStateListAdapterListener
      private var networkState: NetworkState = NetworkState.LOADING
  
      val adapterCallback = AdapterListUpdateCallback(this)
  
      val listUpdateCallback = object : ListUpdateCallback {
          override fun onInserted(position: Int, count: Int) {
              adapterCallback.onInserted(position + 1, count)
          }
  
          override fun onRemoved(position: Int, count: Int) {
              adapterCallback.onRemoved(position + 1, count)
          }
  
          override fun onMoved(fromPosition: Int, toPosition: Int) {
              adapterCallback.onMoved(fromPosition + 1, toPosition + 1)
          }
  
          override fun onChanged(position: Int, count: Int, payload: Any?) {
              adapterCallback.onChanged(position + 1, count, payload)
          }
      }
  
      val differ = AsyncPagedListDiffer&lt;MerchantHistoryResponse.Transaction&gt;(listUpdateCallback,
          AsyncDifferConfig.Builder&lt;MerchantHistoryResponse.Transaction&gt;(MerchantHistoryDiffUtilCallback()).build())
  
      override fun getItem(position: Int): MerchantHistoryResponse.Transaction? {
          return differ.getItem(position - 1)
      }
  
      override fun submitList(pagedList: PagedList&lt;MerchantHistoryResponse.Transaction&gt;?) {
          differ.submitList(pagedList)
      }
  
      override fun getCurrentList(): PagedList&lt;MerchantHistoryResponse.Transaction&gt;? {
          return differ.currentList
      }
  
      override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
          holder.onBind(position)
      }
  
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
          return when (viewType) {
              R.layout.item_merchant_history_header -&gt; {
                  val view = ItemMerchantHistoryHeaderBinding.inflate(LayoutInflater.from(parent.context))
                  MerchantHistoryHeaderViewHolder(view)
              }
              R.layout.item_merchant_history -&gt; {
                  val view = ItemMerchantHistoryBinding.inflate(LayoutInflater.from(parent.context))
                  MerchantHistoryViewHolder(view)
              }
              R.layout.item_network_state -&gt; {
                  val view = ItemNetworkStateBinding.inflate(LayoutInflater.from(parent.context))
                  NetworkStateViewHolder(view)
              }
              else -&gt; throw IllegalArgumentException("unknown view type $viewType")
          }
      }
  
      override fun getItemViewType(position: Int): Int {
          return if(position == 0){
              R.layout.item_merchant_history_header
          }
          else if (hasExtraRow() &amp;&amp; position == itemCount - 1) {
              R.layout.item_network_state
          } else {
              R.layout.item_merchant_history
          }
      }
  
      private fun hasExtraRow() = networkState != null &amp;&amp; networkState != NetworkState.LOADED
  
      override fun getItemCount(): Int {
          //Header item, plus the extra row
          return super.getItemCount() + if (hasExtraRow()) 2 else 1
      }
  
      fun setNetworkState(newNetworkState: NetworkState) {
          Timber.d("Setting network state....." + newNetworkState.status)
          val previousState = this.networkState
          val hadExtraRow = hasExtraRow()
          this.networkState = newNetworkState
          val hasExtraRow = hasExtraRow()
          if (hadExtraRow != hasExtraRow) {
              if (hadExtraRow) {
                  notifyItemRemoved(differ.itemCount + 1)
              } else {
                  notifyItemInserted(differ.itemCount + 1)
              }
          } else if (hasExtraRow &amp;&amp; previousState != newNetworkState) {
              notifyItemChanged(itemCount - 1)
          }
      }
  
      inner class MerchantHistoryViewHolder(private val binding: ItemMerchantHistoryBinding)
          : BaseViewHolder(binding.root), MerchantHistoryItemViewModel.ListItemViewModelListener {
  
          private var itemViewModel: MerchantHistoryItemViewModel? = null
  
          override fun onBind(position: Int) {
              val transaction = getItem(position)
  
              transaction?.let {
                  itemViewModel = MerchantHistoryItemViewModel(binding.root.context, it, this)
                  binding.viewModel = itemViewModel
                  binding.executePendingBindings()
              }
          }
  
          override fun onItemClick(item: MerchantHistoryResponse.Transaction) {
              listener.onItemClick(item)
          }
      }
  
      interface HistoryItemListAdapterListener {
          fun onItemClick(item: MerchantHistoryResponse.Transaction)
      }
  
      inner class NetworkStateViewHolder(private val binding: ItemNetworkStateBinding)
          : BaseViewHolder(binding.root), NetworkStateItemViewModel.ListItemViewModelListener {
  
          private var itemViewModel: NetworkStateItemViewModel? = null
  
          override fun onBind(position: Int) {
              Timber.d("binding position " + position + " with network state of " + networkState.status)
              itemViewModel = NetworkStateItemViewModel(binding.root.context, networkState,this)
              binding.viewModel = itemViewModel
              binding.executePendingBindings()
          }
  
          override fun onRetryClick() {
              networkStateListener.onRetryClick()
          }
      }
  
      interface NetworkStateListAdapterListener {
          fun onRetryClick()
      }
  
      inner class MerchantHistoryHeaderViewHolder(private val binding: ItemMerchantHistoryHeaderBinding)
          : BaseViewHolder(binding.root), MerchantHistoryHeaderItemViewModel.ListItemViewModelListener {
  
          private var itemViewModel: MerchantHistoryHeaderItemViewModel? = null
  
          override fun onBind(position: Int) {
              itemViewModel = MerchantHistoryHeaderItemViewModel(binding.root.context, this)
              binding.viewModel = itemViewModel
              binding.executePendingBindings()
          }
  
          override fun onItemClick() {
              headerListener.onItemClick()
          }
      }
  
      interface HistoryItemHeaderListAdapterListener {
          fun onItemClick()
      }
  
      class MerchantHistoryDiffUtilCallback : DiffUtil.ItemCallback&lt;MerchantHistoryResponse.Transaction&gt;() {
  
          override fun areItemsTheSame(oldItem: MerchantHistoryResponse.Transaction, newItem: MerchantHistoryResponse.Transaction): Boolean {
              return oldItem.uid == newItem.uid
          }
  
          override fun areContentsTheSame(oldItem: MerchantHistoryResponse.Transaction, newItem: MerchantHistoryResponse.Transaction): Boolean {
              return oldItem.uid == newItem.uid
          }
      }
  }
  </code></pre>