title: RecyclerView SnapHelper - how to change background of snapped and unsnapped items?
body: <p>I'm using recyclerview with horizontal LinearLayoutManager. I want to change recyclerview item color, when item is snapped in a middle.</p>
  
  <p>So i used LinearSnapHelper.</p>
  
  <p>At the moment, each items, which were previously snapped stays with snapped background color. So What i did, I made global variable snappedPosition and set it to 0, so first item of the recyclerview will be snapped by default.</p>
  
  <p>Then I called that snappedPosition in onBindViewHolder of my adapter and changed color of item to default for every item in recyclerview except one with same position as position of snapped item. But at notifyItemChanged(position) ive got:</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling android.support.v7.widget.RecyclerView</p>
  </blockquote>
  
  <p>code inside fragment:</p>
  
  <pre><code>val snapHelper: LinearSnapHelper = LinearSnapHelper()
  var snappedPosition: Int = 0
  
          addressRecyclerView = view.findViewById&lt;RecyclerView&gt;(R.id.address_recycler_view).apply {
  
              setHasFixedSize(true)
              layoutManager = viewManager
              adapter = viewAdapter
  
          }
  
          snapHelper.attachToRecyclerView(addressRecyclerView)
          addressRecyclerView.onFlingListener = snapHelper
          itemOffsetDecoration = ItemOffsetDecoration(context, R.dimen.address_recyclerview_padding)
          addressRecyclerView.addItemDecoration(itemOffsetDecoration)
  
          addressRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
              override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                  if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                      val centerView = snapHelper.findSnapView(viewManager)
                      if (centerView != null) {
  
                          snappedPosition = addressRecyclerView.getChildAdapterPosition(centerView)
                      }
                  }
              }
          })
  </code></pre>
  
  <p>code inside onBindViewHolder</p>
  
  <pre><code>addressList[position].let {
  
  
                  if (position == fragment.snappedPosition){
                      holder.itemViewLayout.setBackgroundResource(R.drawable.payment_button_active)
                      notifyItemChanged(position)
                  } else {
                      holder.itemViewLayout.setBackgroundResource(R.drawable.payment_button_inactive)
                      notifyItemChanged(position)
                  }
  
              }
  </code></pre>
  
  <p>What i want to achieve is similar to this image example:</p>
  
  <p><a href="https://i.stack.imgur.com/18dkZ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/18dkZ.jpg" alt="enter image description here"></a></p>
  
  <p>Update: Code example for color change variant (still not working):</p>
  
  <pre><code>addressRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
              override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                  if (newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                      val centerView = snapHelper.findSnapView(addressRecyclerView.layoutManager)
                      if (centerView != null) {
                          for(item in 0 until addressRecyclerView.layoutManager.childCount){
                              val holder: RecyclerView.ViewHolder = addressRecyclerView.findViewHolderForAdapterPosition(item)
                              holder.itemView.setBackgroundResource(R.drawable.payment_button_inactive)
                          }
                          centerView.setBackgroundResource(R.drawable.payment_button_active)
  
                      }
                  }
              }
          })
  </code></pre>