private void updateOrClearHeader(int firstVisiblePosition) {
    final int adapterCount = mAdapter == null ? 0 : mAdapter.getCount();
    if (adapterCount == 0 || !mAreHeadersSticky) {
      return;
    }

    final int headerViewCount = mList.getHeaderViewsCount();
    final int realFirstVisibleItem = firstVisiblePosition - headerViewCount;

    // It is not a mistake to call getFirstVisiblePosition() here.
    // Most of the time getFixedFirstVisibleItem() should be called
    // but that does not work great together with getChildAt()
    final boolean doesListHaveChildren = mList.getChildCount() != 0;
    final boolean isFirstViewBelowTop =
        doesListHaveChildren
            && mList.getFirstVisiblePosition() == 0
            && mList.getChildAt(0).getTop() > 0;
    final boolean isFirstVisibleItemOutsideAdapterRange =
        realFirstVisibleItem > adapterCount - 1 || realFirstVisibleItem < 0;
    if (!doesListHaveChildren || isFirstVisibleItemOutsideAdapterRange || isFirstViewBelowTop) {
      clearHeader();
      return;
    }

    updateHeader(realFirstVisibleItem);
  }
 public int getFirstVisiblePosition() {
   return mList.getFirstVisiblePosition();
 }