/**
     * This method takes layout manager and finds the right center element of it
     *
     * @param layoutManager
     * @return View
     */
    private View findCenterView(LinearLayoutManager layoutManager) {

      int minDistance = 0;
      View view, returnView = null;
      boolean notFound = true;

      for (int i = layoutManager.findFirstVisibleItemPosition();
          i <= layoutManager.findLastVisibleItemPosition() && notFound;
          i++) {

        view = layoutManager.findViewByPosition(i);

        int center = (view.getLeft() + view.getRight()) / 2;

        int leastDifference = Math.abs(mCenterPivot - center);

        if (leastDifference <= minDistance || i == layoutManager.findFirstVisibleItemPosition()) {
          minDistance = leastDifference;
          returnView = view;
        } else {
          notFound = false;
        }
      }
      return returnView;
    }
예제 #2
0
  private View findCenterView(LinearLayoutManager lm) {

    int minDistance = 0;
    View view = null;
    View returnView = null;
    boolean notFound = true;

    for (int i = lm.findFirstVisibleItemPosition();
        i <= lm.findLastVisibleItemPosition() && notFound;
        i++) {

      view = lm.findViewByPosition(i);

      int center =
          lm.getOrientation() == LinearLayoutManager.HORIZONTAL
              ? (view.getLeft() + view.getRight()) / 2
              : (view.getTop() + view.getBottom()) / 2;
      int leastDifference = Math.abs(mCenterPivot - center);

      if (leastDifference <= minDistance || i == lm.findFirstVisibleItemPosition()) {
        minDistance = leastDifference;
        returnView = view;
        setCenterView(returnView);
      } else {
        notFound = false;
      }
    }
    return returnView;
  }
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
      super.onScrollStateChanged(recyclerView, newState);

      LinearLayoutManager linearLayoutManager =
          (LinearLayoutManager) recyclerView.getLayoutManager();

      if (mCenterPivot == 0) {
        mCenterPivot = recyclerView.getLeft() + recyclerView.getRight();
      }
      if (!mAutoSet) {

        synchronized (this) {
          if (newState == RecyclerView.SCROLL_STATE_IDLE) {

            View view = linearLayoutManager.findViewByPosition(mPosition);

            mPosition = INITIAL_POSITION;

            if (view == null) {
              view = findCenterView(linearLayoutManager);
            }
            int viewCenter = (view.getLeft() + view.getRight()) / 2;
            int viewWidth = view.getRight() - view.getLeft();

            int scrollNeeded = viewCenter - mCenterPivot;

            if (BuildConfig.DEBUG)
              Log.d(
                  TAG,
                  "viewCentre = "
                      + viewCenter
                      + " viewWidth = "
                      + viewWidth
                      + " scrollNeeded = "
                      + scrollNeeded
                      + " recyclerViewWidth = "
                      + recyclerView.getMeasuredWidth());

            recyclerView.smoothScrollBy(scrollNeeded, 0);

            mCenterHolderPosition = recyclerView.getChildLayoutPosition(view);

            if (BuildConfig.DEBUG)
              Log.d(
                  TAG,
                  "centerHolderPosition = " + mCenterHolderPosition + " mPosition = " + mPosition);

            if (mScrollType == null) {
              mScrollType = SwipeType.SWIPE;
            }

            onCenterItemSnap(
                mCenterHolderPosition, recyclerView.getChildViewHolder(view), mScrollType);

            mScrollType = null;
            mAutoSet = true;
          }
        }
      }
      if (newState == RecyclerView.SCROLL_STATE_DRAGGING
          || newState == RecyclerView.SCROLL_STATE_SETTLING) {
        mAutoSet = false;
      }
    }