@Override public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) { if (parent.isAnimating()) return; final int childCount = parent.getChildCount(); final RecyclerView.LayoutManager lm = parent.getLayoutManager(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); RecyclerView.ViewHolder viewHolder = parent.getChildViewHolder(child); if (requiresDivider(viewHolder)) { final int right = lm.getDecoratedRight(child); final int bottom = lm.getDecoratedBottom(child); // draw the bottom divider canvas.drawRect(lm.getDecoratedLeft(child), bottom - dividerSize, right, bottom, paint); // draw the right edge divider canvas.drawRect(right - dividerSize, lm.getDecoratedTop(child), right, bottom - dividerSize, paint); } } }
@Override public int findTargetSnapPosition(RecyclerView.LayoutManager lm, int velocityX, int velocityY) { final int itemCount = lm.getItemCount(); if (itemCount == 0) { return RecyclerView.NO_POSITION; } final int childCount = lm.getChildCount(); if (childCount == 0) { return RecyclerView.NO_POSITION; } final boolean leftDirection = velocityX > 0; if (leftDirection) { final View leftChild = lm.getChildAt(0); final int leftChildPos = lm.getPosition(leftChild); final int leftChildRight = lm.getDecoratedRight(leftChild); return Math.min(itemCount - 1, leftChildPos + (leftChildRight > 0 ? 1 : 2)); } else { final View rightChild = lm.getChildAt(childCount - 1); final int rightChildPos = lm.getPosition(rightChild); return Math.max(0, rightChildPos - 1); } }
? view.getWidth() - layoutManager.getPaddingRight() - layoutManager.getDecoratedRight(firstView) : layoutManager.getDecoratedLeft(firstView) - layoutManager.getPaddingLeft(); } else {
/** * Calculates the horizontal scroll amount necessary to make the given view fully visible * inside the RecyclerView. * * @param view The view which we want to make fully visible * @param snapPreference The edge which the view should snap to when entering the visible * area. One of {@link #SNAP_TO_START}, {@link #SNAP_TO_END} or * {@link #SNAP_TO_END} * @return The vertical scroll amount necessary to make the view visible with the given * snap preference. */ public int calculateDxToMakeVisible(View view, int snapPreference) { final RecyclerView.LayoutManager layoutManager = getLayoutManager(); if (layoutManager == null || !layoutManager.canScrollHorizontally()) { return 0; } final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams(); final int left = layoutManager.getDecoratedLeft(view) - params.leftMargin; final int right = layoutManager.getDecoratedRight(view) + params.rightMargin; final int start = layoutManager.getPaddingLeft(); final int end = layoutManager.getWidth() - layoutManager.getPaddingRight(); return calculateDtToFit(left, right, start, end, snapPreference); }
/** * Calculates the horizontal scroll amount necessary to make the given view in center of the RecycleView * * @param view The view which we want to make in center of the RecycleView * @return The horizontal scroll amount necessary to make the view in center of the RecycleView */ public int calculateDxToMakeCentral(View view) { final RecyclerView.LayoutManager layoutManager = getLayoutManager(); if (layoutManager == null || !layoutManager.canScrollHorizontally()) { return 0; } final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams(); final int left = layoutManager.getDecoratedLeft(view) - params.leftMargin; final int right = layoutManager.getDecoratedRight(view) + params.rightMargin; final int start = layoutManager.getPaddingLeft(); final int end = layoutManager.getWidth() - layoutManager.getPaddingRight(); final int childCenter = left + (int) ((right - left) / 2.0f); final int containerCenter = (int) ((end - start) / 2.f); return containerCenter - childCenter; }
private int adjustDxForLeft(int dx) { final RecyclerView.LayoutManager layoutManager = getLayoutManager(); if (layoutManager == null || !layoutManager.canScrollHorizontally()) { return 0; } final View lastChild = layoutManager.getChildAt(layoutManager.getChildCount() - 1); final int position = layoutManager.getPosition(lastChild); if (position == layoutManager.getItemCount() - 1) { final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) lastChild.getLayoutParams(); final int maxDx = layoutManager.getWidth() - layoutManager.getPaddingRight() - (layoutManager.getDecoratedRight(lastChild) + params.rightMargin); if (dx < maxDx) { return maxDx; } } return dx; }
@Override public int getDecoratedEnd(View view) { final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams(); return mLayoutManager.getDecoratedRight(view) + params.rightMargin; }
@Override public int getDecoratedEnd(View view) { final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams(); return mLayoutManager.getDecoratedRight(view) + params.rightMargin; }
@Override public int getDecoratedEnd(View view) { final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams(); return mLayoutManager.getDecoratedRight(view) + params.rightMargin; }