@Override
  public void setSelection(int position) {
    if (adapter == null) {
      return;
    }

    Assert.assertTrue("Invalid selection position", position >= 0 && position < adapterDataCount);

    releaseViews();

    View selectedView = viewFromAdapter(position, true);
    bufferedViews.add(selectedView);

    for (int i = 1; i <= sideBufferSize; i++) {
      int previous = position - i;
      int next = position + i;

      if (previous >= 0) {
        bufferedViews.addFirst(viewFromAdapter(previous, false));
      }
      if (next < adapterDataCount) {
        bufferedViews.addLast(viewFromAdapter(next, true));
      }
    }

    bufferIndex = bufferedViews.indexOf(selectedView);
    adapterIndex = position;

    requestLayout();
    updateVisibleView(inFlipAnimation ? -1 : bufferIndex);

    cards.resetSelection(position, adapterDataCount);
  }
  private void onDataChanged() {
    adapterDataCount = adapter.getCount();
    int activeIndex;
    if (adapterIndex < 0) {
      activeIndex = 0;
    } else {
      activeIndex = Math.min(adapterIndex, adapterDataCount - 1);
    }

    releaseViews();
    setSelection(activeIndex);
  }