Java Code Examples for android.support.v7.widget.RecyclerView#getChildAdapterPosition()

The following examples show how to use android.support.v7.widget.RecyclerView#getChildAdapterPosition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GridSpacesItemDecoration.java    From Loop with Apache License 2.0 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

    if(parent.getChildAdapterPosition(view) == 0 || parent.getChildAdapterPosition(view) == 1){
        outRect.top = space;
    }

    if(parent.getChildAdapterPosition(view) % 2 == 0){
        outRect.left = space;
        outRect.right = space/2;
    } else {
        outRect.left = space/2;
        outRect.right = space;
    }

    outRect.bottom = space;
}
 
Example 2
Source File: DividerItemDecoration.java    From AndroidSchool with Apache License 2.0 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                           RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);
    if (mDivider == null) {
        return;
    }

    int position = parent.getChildAdapterPosition(view);
    if (position == RecyclerView.NO_POSITION || (position == 0)) {
        return;
    }

    if (mOrientation == -1) {
        getOrientation(parent);
    }

    if (mOrientation == LinearLayoutManager.VERTICAL) {
        outRect.top = mDivider.getIntrinsicHeight();
    } else {
        outRect.left = mDivider.getIntrinsicWidth();
    }
}
 
Example 3
Source File: FlingBehavior.java    From AmazingAvatar with MIT License 6 votes vote down vote up
@Override
public boolean onNestedFling(@NonNull CoordinatorLayout coordinatorLayout,
                             @NonNull AppBarLayout child, @NonNull View target, float velocityX,
                             float velocityY, boolean consumed) {
	if (velocityY > 0 && !isPositive || velocityY < 0 && isPositive) {
		velocityY = velocityY * -1;
	}
	if (target instanceof RecyclerView && velocityY < 0) {
		final RecyclerView recyclerView = (RecyclerView) target;
		final View firstChild = recyclerView.getChildAt(0);
		final int childAdapterPosition = recyclerView.getChildAdapterPosition(firstChild);
		consumed = childAdapterPosition > TOP_CHILD_FLING_THRESHOLD;
	}
	return super
			.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
}
 
Example 4
Source File: GridSpaceDecoration.java    From ItemDecorationDemo with Apache License 2.0 6 votes vote down vote up
/**
 * orientation为Vertical时调用,处理Vertical下的Offset
 * {@link #getItemOffsets(Rect, View, RecyclerView, RecyclerView.State)}
 */
private void handleVertical(Rect outRect, View view, RecyclerView parent,
                            RecyclerView.State state) {
    GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) view.getLayoutParams();
    int childPos = parent.getChildAdapterPosition(view);
    int sizeAvg = (int) ((mHorizontal * (mSpanCount - 1) + mLeft + mRight) * 1f / mSpanCount);
    int spanSize = lp.getSpanSize();
    int spanIndex = lp.getSpanIndex();
    outRect.left = computeLeft(spanIndex, sizeAvg);
    if (spanSize == 0 || spanSize == mSpanCount) {
        outRect.right = sizeAvg - outRect.left;
    } else {
        outRect.right = computeRight(spanIndex + spanSize - 1, sizeAvg);
    }
    outRect.top = mVertical / 2;
    outRect.bottom = mVertical / 2;
    if (isFirstRaw(childPos)) {
        outRect.top = mTop;
    }
    if (isLastRaw(childPos)) {
        outRect.bottom = mBottom;
    }
}
 
Example 5
Source File: HeaderDecoration.java    From recyclerviewItemDecorations with MIT License 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if (parent.getChildAdapterPosition(view) < mColumns) {
        if (mHorizontal) {
            if (mView.getMeasuredWidth() <= 0) {
                mView.measure(View.MeasureSpec.makeMeasureSpec(parent.getMeasuredWidth(), View.MeasureSpec.AT_MOST),
                        View.MeasureSpec.makeMeasureSpec(parent.getMeasuredHeight(), View.MeasureSpec.AT_MOST));
            }
            outRect.set(mView.getMeasuredWidth(), 0, 0, 0);
        } else {
            if (mView.getMeasuredHeight() <= 0) {
                mView.measure(View.MeasureSpec.makeMeasureSpec(parent.getMeasuredWidth(), View.MeasureSpec.AT_MOST),
                        View.MeasureSpec.makeMeasureSpec(parent.getMeasuredHeight(), View.MeasureSpec.AT_MOST));
            }
            outRect.set(0, mView.getMeasuredHeight(), 0, 0);
        }
    } else {
        outRect.setEmpty();
    }
}
 
Example 6
Source File: StaggeredGridDecoration.java    From ReadMark with Apache License 2.0 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    outRect.bottom = bottom;
    //第一排
    if(parent.getChildAdapterPosition(view) < spanCount){
        outRect.top = 2 * top;
    }else{
        outRect.top = top;
    }
    //第一列
    if(parent.getChildAdapterPosition(view) % spanCount == 0){
        outRect.left = 2 * left;
        outRect.right = right;
    }else{
        outRect.left = left;
        outRect.right = 2 * right;
    }
}
 
Example 7
Source File: GridDividerItemDecoration.java    From PowerFileExplorer with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Determines the size and location of offsets between items in the parent
   * RecyclerView.
   *
   * @param outRect The {@link Rect} of offsets to be added around the child view
   * @param view The child view to be decorated with an offset
   * @param parent The RecyclerView onto which dividers are being added
   * @param state The current RecyclerView.State of the RecyclerView
   */
  @Override
  public void getItemOffsets(final Rect outRect, final View view, final RecyclerView parent, final RecyclerView.State state) {
      super.getItemOffsets(outRect, view, parent, state);

if (mNumColumns <= 0) {
	mNumColumns = ((GridLayoutManager)parent.getLayoutManager()).getSpanCount();
}
final boolean childIsInLeftmostColumn = (parent.getChildAdapterPosition(view) % mNumColumns) == 0;
      if (!childIsInLeftmostColumn && mHorizontalDivider != null) {
          outRect.left = mHorizontalDivider.getIntrinsicWidth();
      }

      final boolean childIsInFirstRow = (parent.getChildAdapterPosition(view)) < mNumColumns;
      if (!childIsInFirstRow && mVerticalDivider != null) {
          outRect.top = mVerticalDivider.getIntrinsicHeight();
      }
  }
 
Example 8
Source File: GridItemDecoration.java    From EasyAdapter with Apache License 2.0 5 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int position = parent.getChildAdapterPosition(view);

    int spanCount = getSpanCount(parent);
    int childCount = parent.getAdapter().getItemCount();

    int pos = position;

    if(hasHeader || hasFooter) {
        if(position == 0 && hasHeader) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
            return;
        }
        if(position == state.getItemCount()-1 && hasFooter){
            outRect.set(0, 0, 0, 0);
            return;
        }else {
            pos = position - 1;
        }
    }

    if (isLastColum(parent, pos, spanCount, childCount)) {
        outRect.set(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight());
    } else {
        outRect.set(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight());
    }
}
 
Example 9
Source File: DoubleHeaderDecoration.java    From LRecyclerView with Apache License 2.0 5 votes vote down vote up
private int getSubHeaderTop(RecyclerView parent, View child, View header, View subHeader, int adapterPos, int layoutPos) {
    int top = getAnimatedTop(child) - subHeader.getHeight();
    if (isFirstValidChild(layoutPos, parent)) {
        final int count = parent.getChildCount();
        final long currentHeaderId = mAdapter.getHeaderId(adapterPos);
        final long currentSubHeaderId = mAdapter.getSubHeaderId(adapterPos);

        // find next view with sub-header and compute the offscreen push if needed
        for (int i = layoutPos + 1; i < count; i++) {
            final View next = parent.getChildAt(i);
            int adapterPosHere = parent.getChildAdapterPosition(next);
            if (adapterPosHere != RecyclerView.NO_POSITION) {
                final long nextHeaderId = mAdapter.getHeaderId(adapterPosHere);
                final long nextSubHeaderId = mAdapter.getSubHeaderId(adapterPosHere);

                if ((nextSubHeaderId != currentSubHeaderId)) {
                    int headersHeight = subHeader.getHeight() + getSubHeader(parent, adapterPosHere).itemView.getHeight();
                    if (nextHeaderId != currentHeaderId) {
                        headersHeight += getHeader(parent, adapterPosHere).itemView.getHeight();
                    }

                    final int offset = getAnimatedTop(next) - headersHeight;
                    if (offset < header.getHeight()) {
                        return offset;
                    } else {
                        break;
                    }
                }
            }
        }
    }

    return Math.max(header.getHeight(), top);
}
 
Example 10
Source File: DividerItemDecoration.java    From PracticalRecyclerView with Apache License 2.0 5 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                           RecyclerView.State state) {
    int position = parent.getChildAdapterPosition(view);
    AbstractAdapter adapter = (AbstractAdapter) parent.getAdapter();
    if (adapter.isData(position)) {
        getItemOffset(outRect);
    }
}
 
Example 11
Source File: RvItemDecoration.java    From RetrofitClient with MIT License 5 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    outRect.left = space;
    outRect.right = space;
    outRect.bottom = space;
    if (parent.getChildAdapterPosition(view) == 0) {
        outRect.top = space;
    }
}
 
Example 12
Source File: LoadMoreOnScrollListener.java    From RecyclerViewTools with Apache License 2.0 5 votes vote down vote up
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

      if (!ViewCompat.canScrollVertically(recyclerView, -1)) {
         loadMoreListenerCalled = false;
      }

      try {

         // don't call listener if adapter is empty
         RecyclerView.Adapter a = recyclerView.getAdapter();
         if (a instanceof WrapAdapter && ((WrapAdapter) a).getWrappedCount() == 0)
            return;
         else if (a.getItemCount() == 0) return;

         // get position for last childView on recyclerView
         int position = recyclerView.getChildAdapterPosition(recyclerView.getChildAt(recyclerView.getChildCount() - 1));

         // if recyclerView don't know the position, there's nothing I can do
         if (position == RecyclerView.NO_POSITION) return;

         // check offset to call listener
         if (position >= recyclerView.getAdapter().getItemCount() - positionOffset) {
            if (!loadMoreListenerCalled) {
               listener.onLoadMore(recyclerView);
               loadMoreListenerCalled = true;
            }
         } else {
            loadMoreListenerCalled = false;
         }

      } catch (NullPointerException e) {
         /* shouldn't happen, but you never know */
         loadMoreListenerCalled = false;
      }
   }
 
Example 13
Source File: HeaderSpaceItemDecoration.java    From zulip-android with Apache License 2.0 5 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                           RecyclerView.State state) {

    int position = parent.getChildAdapterPosition(view);
    int size = parent.getAdapter().getItemCount();
    int viewType = parent.getAdapter().getItemViewType(position);
    if (viewType == RecyclerMessageAdapter.VIEWTYPE_HEADER) {
        outRect.top = toolbarHeight;
    }
    outRect.bottom = 0;
}
 
Example 14
Source File: ViewUtils.java    From oneHookLibraryAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * Get position of center child in X Axes
 */
public static int getCenterXChildPosition(RecyclerView recyclerView) {
    int childCount = recyclerView.getChildCount();
    if (childCount > 0) {
        for (int i = 0; i < childCount; i++) {
            View child = recyclerView.getChildAt(i);
            if (isChildInCenterX(recyclerView, child)) {
                return recyclerView.getChildAdapterPosition(child);
            }
        }
    }
    return childCount;
}
 
Example 15
Source File: StaggeredLayoutManagerUtils.java    From RecyclerViewTools with Apache License 2.0 5 votes vote down vote up
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

         View v1 = recyclerView.getChildAt(0);
         View v2 = recyclerView.getChildAt(recyclerView.getChildCount() - 1);

         int i1 = recyclerView.getChildAdapterPosition(v1);
         int i2 = recyclerView.getChildAdapterPosition(v2);

         if (position >= i1 && position <= i2) {
            ((StaggeredGridLayoutManager) recyclerView.getLayoutManager()).invalidateSpanAssignments();
            recyclerView.invalidateItemDecorations();
            recyclerView.removeOnScrollListener(this);
         }
      }
 
Example 16
Source File: StickyNormalItemLine.java    From YCRefreshView with Apache License 2.0 5 votes vote down vote up
private int getHeaderTop(RecyclerView parent, View child, View header,
                         int adapterPos, int layoutPos) {
    int headerHeight = getHeaderHeightForLayout(header);
    int top = ((int) child.getY()) - headerHeight;
    if (layoutPos == 0) {
        final int count = parent.getChildCount();
        final long currentId = mAdapter.getHeaderId(adapterPos);
        // find next view with header and compute the offscreen push if needed
        for (int i = 1; i < count; i++) {
            int adapterPosHere = parent.getChildAdapterPosition(parent.getChildAt(i));
            if (adapterPosHere != RecyclerView.NO_POSITION) {
                long nextId = mAdapter.getHeaderId(adapterPosHere);
                if (nextId != currentId) {
                    final View next = parent.getChildAt(i);
                    final int offset = ((int) next.getY()) - (headerHeight +
                            getHeader(parent, adapterPosHere).itemView.getHeight());
                    if (offset < 0) {
                        return offset;
                    } else {
                        break;
                    }
                }
            }
        }
        top = Math.max(0, top);
    }
    return top;
}
 
Example 17
Source File: CircularUtils.java    From CircularViewPager with MIT License 5 votes vote down vote up
/**
 * get the position of the center RecyclerView
 *
 * @param recyclerView
 * @return
 */
public static int getCenterXChildPosition(@NonNull RecyclerView recyclerView) {
    int childCount = recyclerView.getChildCount();
    if (childCount > 0) {
        for (int i = 0; i < childCount; i++) {
            View child = recyclerView.getChildAt(i);
            if (isChildInCenterX(recyclerView, child)) {
                return recyclerView.getChildAdapterPosition(child);
            }
        }
    }
    return childCount;
}
 
Example 18
Source File: SlidingMenuDivider.java    From uPods-android with Apache License 2.0 5 votes vote down vote up
/**
 * Custom divider will be used
 */

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int position = parent.getChildAdapterPosition(view);
    SlidingMenuAdapter adapter = (SlidingMenuAdapter) parent.getAdapter();
    if (adapter.shouldHideDivider(position)) {
        outRect.set(0, 0, 0, 0);
    } else {
        outRect.bottom = DEVIDER_SIZE;
    }
}
 
Example 19
Source File: BaseDivider.java    From TitanRecyclerView with MIT License 4 votes vote down vote up
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    RecyclerView.Adapter adapter = parent.getAdapter();
    if (adapter == null) {
        return;
    }

    int itemCount = adapter.getItemCount();
    int lastDividerOffset = getLastDividerOffset(parent);
    int validChildCount = parent.getChildCount();
    int lastChildPosition = -1;
    for (int i = 0; i < validChildCount; i++) {
        View child = parent.getChildAt(i);
        int childPosition = parent.getChildAdapterPosition(child);

        if (childPosition < lastChildPosition) {
            // Avoid remaining divider when animation starts
            continue;
        }
        lastChildPosition = childPosition;

        if (!mShowLastDivider && childPosition >= itemCount - lastDividerOffset) {
            // Don't draw divider for last line if mShowLastDivider = false
            continue;
        }

        if (wasDividerAlreadyDrawn(childPosition, parent)) {
            // No need to draw divider again as it was drawn already by previous column
            continue;
        }

        int groupIndex = getGroupIndex(childPosition, parent);
        if (mVisibilityProvider.shouldHideDivider(groupIndex, parent)) {
            continue;
        }

        Rect bounds = getDividerBound(groupIndex, parent, child);
        switch (mDividerType) {
            case DRAWABLE:
                Drawable drawable = mDrawableProvider.drawableProvider(groupIndex, parent);
                drawable.setBounds(bounds);
                drawable.draw(c);
                break;
            case PAINT:
                mPaint = mPaintProvider.dividerPaint(groupIndex, parent);
                c.drawLine(bounds.left, bounds.top, bounds.right, bounds.bottom, mPaint);
                break;
            case COLOR:
                mPaint.setColor(mColorProvider.dividerColor(groupIndex, parent));
                mPaint.setStrokeWidth(mSizeProvider.dividerSize(groupIndex, parent));
                c.drawLine(bounds.left, bounds.top, bounds.right, bounds.bottom, mPaint);
                break;
        }
    }
}
 
Example 20
Source File: RecyclerViewWithInset.java    From oneHookLibraryAndroid with Apache License 2.0 4 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                           RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);

    /*
     * find the position of the child, and figure out if the child is at
     * the first row
     */
    final int position = parent.getChildAdapterPosition(view);

    if (parent.getLayoutManager() instanceof GridLayoutManager) {
        /*
         * if using grid layout manager
         */
        final GridLayoutManager.SpanSizeLookup sizeLookUp = ((GridLayoutManager) parent
                .getLayoutManager()).getSpanSizeLookup();
        final int span = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();
        final int groupIndex = sizeLookUp.getSpanGroupIndex(position, span);
        if (groupIndex == 0) {
            outRect.top = mTopContentInset;
        } else {
            final int lastRowGroupIndex = sizeLookUp.getSpanGroupIndex(parent.getAdapter()
                    .getItemCount() - 1, span);
            if (groupIndex == lastRowGroupIndex) {
                outRect.bottom = mBottomContentInset;
            }
        }
    } else {
        /*
         * if using LinearLayoutManager
         */
        if (position == 0) {
            outRect.top = mTopContentInset;
        }
        if (position == parent.getAdapter().getItemCount() - 1) {
            outRect.bottom = mBottomContentInset;
        }
    }

    outRect.left = mItemMargin / 2;
    outRect.right = mItemMargin / 2;
    outRect.top += mItemMargin / 2;
    outRect.bottom += mItemMargin / 2;
}