@Override
  public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);

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

    if (mCenterPivot == 0) {

      mCenterPivot =
          lm.getOrientation() == LinearLayoutManager.HORIZONTAL
              ? (recyclerView.getLeft() + recyclerView.getRight())
              : (recyclerView.getTop() + recyclerView.getBottom());
    }
    if (!mAutoSet) {

      if (newState == RecyclerView.SCROLL_STATE_IDLE) {

        View view = findCenterView(lm);

        int position = recyclerView.getChildPosition(view);

        Log.i("CenterLockListener", "list-position" + position);

        Toast.makeText(context, "list-position" + position, Toast.LENGTH_SHORT).show();

        int viewCenter =
            lm.getOrientation() == LinearLayoutManager.HORIZONTAL
                ? (view.getLeft() + view.getRight()) / 2
                : (view.getTop() + view.getBottom()) / 2;

        int scrollNeeded = viewCenter - mCenterPivot;

        if (lm.getOrientation() == LinearLayoutManager.HORIZONTAL) {

          recyclerView.smoothScrollBy(scrollNeeded, 0);
        } else {
          recyclerView.smoothScrollBy(0, (int) (scrollNeeded));
        }
        mAutoSet = true;
      }
    }
    if (newState == RecyclerView.SCROLL_STATE_DRAGGING
        || newState == RecyclerView.SCROLL_STATE_SETTLING) {

      mAutoSet = false;
    }
  }
 @Override
 public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
   super.onDraw(c, parent, state);
   // layout basically just gets drawn on the reserved space on top of the first view
   mLayout.layout(parent.getLeft(), 0, parent.getRight(), mLayout.getMeasuredHeight());
   for (int i = 0; i < parent.getChildCount(); i++) {
     View view = parent.getChildAt(i);
     if (parent.getChildAdapterPosition(view) == 0) {
       c.save();
       final int height = mLayout.getMeasuredHeight();
       final int top = view.getTop() - height;
       c.translate(0, top);
       mLayout.draw(c);
       c.restore();
       break;
     }
   }
 }
Beispiel #3
0
 public static View getFirstIntersectsChild(RecyclerView recyclerView) {
   int childCount = recyclerView.getChildCount();
   if (childCount > 0) {
     for (int i = 0; i < childCount; i++) {
       View child = recyclerView.getChildAt(i);
       Rect rect1 =
           new Rect(
               recyclerView.getLeft(),
               recyclerView.getTop(),
               recyclerView.getRight(),
               recyclerView.getBottom());
       Rect rect2 = new Rect(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
       if (Rect.intersects(rect1, rect2)) {
         return child;
       }
     }
   }
   return null;
 }
    @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;
      }
    }