Show selected item on RecyclerView in Sync with ViewPager
<p>I am trying to get a ViewPager and a RecyclerView to work in sync in displaying an image. The ViewPager contains an ImageView which shows a full sized image and the recycler view shows thumbnails of previous and upcoming images.</p>

<p>If the user swipes on the ViewPager then the RecyclerView moves left/right in sync. If the user scrolls and then clicks a photo, the ViewPager sets that position.</p>

<p>This all works, just giving background to my app.</p>

<p>The part I am stuck on is showing a border around the thumbnail that is currently selected in the RecyclerView <b>from onPageSelected</b>. The element which is considered selected is the one that is currently show on the ViewPager</p>

<p>A lot of the other questions deal with this problem using the onClickListener. My onClickListener just calls  </p>

<pre><code>viewPager.setCurrentItem(index)
</code></pre>

<p>So any time an item is selected, onPageSelected is called, either naturally from the ViewPager or from the click event. So this is where I feel I need to set the border.</p>

<p>So far I'm using a bit of a hack since the number of images is only 20.</p>

<pre><code>for (int i = 0, ii = recyclerViewAdapter.getItemCount(); i &lt; ii; i++) {
        //RecyclerView.ViewHolder holder = recyclerView.findViewHolderForItemId(recyclerViewAdapter.getItemId(i));
        RecyclerView.ViewHolder holder = recyclerView.findViewHolderForAdapterPosition(position);

        if (holder instanceof RecyclerViewAdapter.RecyclerViewHolder) {
            if (i == position) {
                ...
                ((RecyclerViewAdapter.RecyclerViewHolder) holder).getViewHolderContainer().setBackground(border);
            } else {
                ((RecyclerViewAdapter.RecyclerViewHolder) holder).getViewHolderContainer().setBackground(null);
            }
        }
    }
</code></pre>

<p>This doesn't work. If get's the right position since I'm provided that by the ViewPager but neither method for retrieving the ViewHolder at 'position' seems to work.</p>

<p>So, after all that, my question is if anyone can recommend me a clean way (or really even a hack at this point) that will let me update the current position of an element based on the position from onPageSelected of the ViewPager.</p>

<p>Thank you kindly.</p>