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

The following examples show how to use android.support.v7.widget.RecyclerView#getChildLayoutPosition() . 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: MainActivity.java    From DouYinWu with MIT License 6 votes vote down vote up
/**
 * 平滑的滑动到指定位置
 */
private void smoothMoveToPosition(RecyclerView mRecyclerView, final int position) {
    // 第一个可见位置
    int firstItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(0));
    // 最后一个可见位置
    int lastItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(mRecyclerView.getChildCount() - 1));
    if (position < firstItem) {
        // 第一种可能:跳转位置在第一个可见位置之前
        mRecyclerView.smoothScrollToPosition(position);
    } else if (position <= lastItem) {
        //滑动指定高度
        mRecyclerView.smoothScrollBy(0, GetScreenWinth.getHeight(this) -
                (Resources.getSystem().getDimensionPixelSize(Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android"))));
    } else {
        // 第三种可能:跳转位置在最后可见项之后
        mRecyclerView.smoothScrollToPosition(position);
    }
}
 
Example 2
Source File: ItemSpaceDecoration.java    From fyber_mobile_offers with MIT License 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    outRect.left = space;
    outRect.bottom = space;

    if (parent.getLayoutManager() instanceof GridLayoutManager) {
        // Add top margin only for the two first items to avoid double space at the beginning of the list
        if (parent.getChildLayoutPosition(view) == 0 || parent.getChildLayoutPosition(view) == 1)
            outRect.top = space;

        if (parent.getChildLayoutPosition(view) % 2 == 1)
            outRect.right = space;
    } else {
        // Add top margin only for the first item to avoid double space at the beginning of the list
        if (parent.getChildLayoutPosition(view) == 0)
            outRect.top = space;

        outRect.right = space;
    }
}
 
Example 3
Source File: ItemDecorationSpaces.java    From DanDanPlayForAndroid with MIT License 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view,
                           RecyclerView parent, RecyclerView.State state) {
    outRect.top = top;
    outRect.left = left;
    outRect.bottom = bottom;
    if (spanCount != 0) {
        int position = parent.getChildLayoutPosition(view);
        if ((position + 1) % spanCount == 0) {
            outRect.right = 0;
        } else {
            outRect.right = right;
        }
    } else {
        outRect.right = right;
    }
}
 
Example 4
Source File: DefaultItemDecoration.java    From SwipeRecyclerView with Apache License 2.0 6 votes vote down vote up
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent,
    @NonNull RecyclerView.State state) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof LinearLayoutManager) {
        int orientation = getOrientation(layoutManager);
        int position = parent.getChildLayoutPosition(view);
        int spanCount = getSpanCount(layoutManager);
        int childCount = layoutManager.getItemCount();

        if (orientation == RecyclerView.VERTICAL) {
            offsetVertical(outRect, position, spanCount, childCount);
        } else {
            offsetHorizontal(outRect, position, spanCount, childCount);
        }
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        outRect.set(mWidth, mHeight, mWidth, mHeight); // |-|-
    }
}
 
Example 5
Source File: ImagePickerActivity.java    From titanium-imagepicker with Apache License 2.0 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int pos = parent.getChildLayoutPosition(view);
    int halfPadding = Math.abs(Defaults.DIVIDER_WIDTH / 2);
    int fullPadding = 2 * halfPadding;

    outRect.top = (pos < Defaults.GRID_SIZE) ? 0 : fullPadding;

    if (pos % Defaults.GRID_SIZE == 0) {      // first column items
        outRect.left = 0;
        outRect.right = halfPadding;

    } else if ( ((pos + 1) % Defaults.GRID_SIZE) == 0 ) {      // last column items
        outRect.right = 0;
        outRect.left = halfPadding;

    } else {    // middle columns items
        outRect.left = halfPadding;
        outRect.right = halfPadding;
    }

    outRect.bottom = 0;
}
 
Example 6
Source File: RxRecyclerViewDividerTool.java    From RxTools-master 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 (space == 0) {
        outRect.left = leftSpace;
        outRect.right = rightSpace;
        outRect.bottom = topSpace;
        outRect.top = bottomSpace;
    } else {
        //不是第一个的格子都设一个左边和底部的间距
        outRect.left = space;
        outRect.right = space;
        outRect.bottom = space;
        outRect.top = space;
    }

    if (isTop) {
        if (parent.getChildLayoutPosition(view) == 0) {
            outRect.top = 0;
        }
        outRect.left = 0;
        outRect.right = 0;
    }
}
 
Example 7
Source File: DividerItemDecoration.java    From ExpandableRecyclerview-Databinding 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 (dividerDrawable == null) {
        return;
    }
    //if first ,return
    if (parent.getChildLayoutPosition(view) < 1) {
        return;
    }
    int layoutOrientation = getOrientation(parent);
    if (layoutOrientation == LinearLayoutManager.VERTICAL) {
        outRect.top = mHeight;
    } else if (layoutOrientation == LinearLayoutManager.HORIZONTAL) {
        outRect.left = mHeight;
    }
}
 
Example 8
Source File: ImagePickerActivity.java    From titanium-imagepicker with Apache License 2.0 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int pos = parent.getChildLayoutPosition(view);
    int halfPadding = Math.abs(Defaults.DIVIDER_WIDTH / 2);
    int fullPadding = 2 * halfPadding;

    outRect.top = (pos < Defaults.GRID_SIZE) ? 0 : fullPadding;

    if (pos % Defaults.GRID_SIZE == 0) {      // first column items
        outRect.left = 0;
        outRect.right = halfPadding;

    } else if ( ((pos + 1) % Defaults.GRID_SIZE) == 0 ) {      // last column items
        outRect.right = 0;
        outRect.left = halfPadding;

    } else {    // middle columns items
        outRect.left = halfPadding;
        outRect.right = halfPadding;
    }

    outRect.bottom = 0;
}
 
Example 9
Source File: RecyclerViewHeader.java    From RecyclerViewHeader with Apache License 2.0 5 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);
    final boolean headerRelatedPosition = parent.getChildLayoutPosition(view) < firstRowSpan;
    int heightOffset = headerRelatedPosition && isVertical ? headerHeight : 0;
    int widthOffset = headerRelatedPosition && !isVertical ? headerWidth : 0;
    if (layoutManager.isReversed()) {
        outRect.bottom = heightOffset;
        outRect.right = widthOffset;
    } else {
        outRect.top = heightOffset;
        outRect.left = widthOffset;
    }
}
 
Example 10
Source File: GridDividerItemDecoration.java    From BaseUIFrame with MIT License 5 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int position = parent.getChildLayoutPosition(view);
    if((position+1) % mSpanCount > 0) {
        outRect.set(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight());
    }else{
        outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
    }
}
 
Example 11
Source File: GridDividerItemDecoration.java    From BaseUIFrame with MIT License 5 votes vote down vote up
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDraw(c, parent, state);
    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        int position = parent.getChildLayoutPosition(child);
        int column = (position + 1) % 3;
        column  = column == 0 ? mSpanCount : column;

        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        final int top = child.getBottom() + params.bottomMargin +
                Math.round(ViewCompat.getTranslationY(child));
        final int bottom = top + mDivider.getIntrinsicHeight();
        final int left = child.getRight() + params.rightMargin +
                Math.round(ViewCompat.getTranslationX(child));
        final int right = left + mDivider.getIntrinsicHeight();

        mDivider.setBounds(child.getLeft(), top, right, bottom);
        mDivider.draw(c);

        if(column < mSpanCount) {
            mDivider.setBounds(left, child.getTop(), right, bottom);
            mDivider.draw(c);
        }

    }
}
 
Example 12
Source File: RecyclerViewHeader.java    From Android-Application-ZJB with Apache License 2.0 5 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);
    int value = (parent.getChildLayoutPosition(view) < mNumberOfChildren) ? mHeaderHeight : 0;
    if (mReversed) {
        outRect.bottom = value;
    } else {
        outRect.top = value;
    }
}
 
Example 13
Source File: GalleryPickerFragment.java    From MediaPickerInstagram with Apache License 2.0 5 votes vote down vote up
private RecyclerView.ItemDecoration addItemDecoration() {
    return new RecyclerView.ItemDecoration() {
        @Override
        public void getItemOffsets(Rect outRect, View view,
                                   RecyclerView parent, RecyclerView.State state) {
            outRect.left = MARGING_GRID;
            outRect.right = MARGING_GRID;
            outRect.bottom = MARGING_GRID;
            if (parent.getChildLayoutPosition(view) >= 0 && parent.getChildLayoutPosition(view) <= 3) {
                outRect.top = MARGING_GRID;
            }
        }
    };
}
 
Example 14
Source File: OnLoadMoreScrollListener.java    From FriendBook with GNU General Public License v3.0 5 votes vote down vote up
private boolean canTriggerLoadMore(RecyclerView recyclerView) {
    View lastChild = recyclerView.getChildAt(recyclerView.getChildCount() - 1);
    int position = recyclerView.getChildLayoutPosition(lastChild);
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    int totalItemCount = layoutManager.getItemCount();
    return totalItemCount - 1 == position;
}
 
Example 15
Source File: DynamicGridView.java    From Android-App-Architecture-MVVM-Databinding with Apache License 2.0 5 votes vote down vote up
@Override
public void getItemOffsets(final Rect outsideRect, final View view, final RecyclerView parent,
        final RecyclerView.State state) {
    // Calculate header count.
    // Since headers may change at runtime so need to re-calculate every time.
    // CAUTION: assume header view type will not be 0.
    final int headerCount = getHeaderCount(parent);

    int position = parent.getChildLayoutPosition(view);

    // No offset for headers.
    if (position < headerCount) {
        outsideRect.top = 0;
        outsideRect.left = 0;
        outsideRect.right = 0;
        outsideRect.bottom = 0;
        return;
    }

    position -= headerCount;
    final int row = position / columnCount; // item row
    final int column = position % columnCount; // item column

    // Add top margin only for the first row to avoid double space between items
    if (row == 0) {
        outsideRect.top = space;
    } else {
        outsideRect.top = 0;
    }

    outsideRect.bottom = space;
    outsideRect.left = offsets[column][0];
    outsideRect.right = offsets[column][1];
}
 
Example 16
Source File: OnLoadMoreScrollListener.java    From youqu_master with Apache License 2.0 5 votes vote down vote up
public boolean canTriggerLoadMore(RecyclerView recyclerView) {
    View lastChild = recyclerView.getChildAt(recyclerView.getChildCount() - 1);
    int position = recyclerView.getChildLayoutPosition(lastChild);
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    int totalItemCount = layoutManager.getItemCount();
    return totalItemCount - 1 == position;
}
 
Example 17
Source File: RecyclerViewItemDecoration.java    From FriendBook with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

    int viewPosition = parent.getChildLayoutPosition(view);

    if (mMode == MODE_HORIZONTAL) {

        if (!(!mLastLineVisible &&
                viewPosition == parent.getAdapter().getItemCount() - 1)) {
            if (mDrawableRid != 0) {
                outRect.set(0, 0, 0, mCurrentThickness);
            } else {
                outRect.set(0, 0, 0, mThickness);
            }
        }

        if (mFirstLineVisible && viewPosition == 0) {
            if (mDrawableRid != 0) {
                outRect.set(0, mCurrentThickness, 0, mCurrentThickness);
            } else {
                outRect.set(0, mThickness, 0, mThickness);
            }
        }

    } else if (mMode == MODE_VERTICAL) {
        if (!(!mLastLineVisible &&
                viewPosition == parent.getAdapter().getItemCount() - 1)) {
            if (mDrawableRid != 0) {
                outRect.set(0, 0, mCurrentThickness, 0);
            } else {
                outRect.set(0, 0, mThickness, 0);
            }
        }
        if (mFirstLineVisible && viewPosition == 0) {
            if (mDrawableRid != 0) {
                outRect.set(mCurrentThickness, 0, mCurrentThickness, 0);
            } else {
                outRect.set(mThickness, 0, mThickness, 0);
            }
        }

    } else if (mMode == MODE_GRID) {
        int columnSize = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();
        int itemSize = parent.getAdapter().getItemCount();

        if (mDrawableRid != 0) {
            setGridOffsets(outRect, viewPosition, columnSize, itemSize, 0);
        } else {
            setGridOffsets(outRect, viewPosition, columnSize, itemSize, 1);
        }
    }

}
 
Example 18
Source File: DragSortRecycler.java    From Muzesto with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Find the new position by scanning through the items on
 * screen and finding the positional relationship.
 * This *seems* to work, another method would be to use
 * getItemOffsets, but I think that could miss items?..
 */
private int getNewPostion(RecyclerView rv) {
    int itemsOnScreen = rv.getLayoutManager().getChildCount();

    float floatMiddleY = floatingItemBounds.top + floatingItemBounds.height() / 2;

    int above = 0;
    int below = Integer.MAX_VALUE;
    for (int n = 0; n < itemsOnScreen; n++) //Scan though items on screen, however they may not
    {                                   // be in order!

        View view = rv.getLayoutManager().getChildAt(n);

        if (view.getVisibility() != View.VISIBLE)
            continue;

        int itemPos = rv.getChildLayoutPosition(view);

        if (itemPos == selectedDragItemPos) //Don't check against itself!
            continue;

        float viewMiddleY = view.getTop() + view.getHeight() / 2;
        if (floatMiddleY > viewMiddleY) //Is above this item
        {
            if (itemPos > above)
                above = itemPos;
        } else if (floatMiddleY <= viewMiddleY) //Is below this item
        {
            if (itemPos < below)
                below = itemPos;
        }
    }
    debugLog("above = " + above + " below = " + below);

    if (below != Integer.MAX_VALUE) {
        if (below < selectedDragItemPos) //Need to count itself
            below++;
        return below - 1;
    } else {
        if (above < selectedDragItemPos)
            above++;

        return above;
    }
}
 
Example 19
Source File: DragSortRecycler.java    From OmniList with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
    debugLog("onInterceptTouchEvent");

    //if (e.getAction() == MotionEvent.ACTION_DOWN)
    {
        View itemView = rv.findChildViewUnder(e.getX(), e.getY());

        if (itemView == null)
            return false;

        boolean dragging = false;

        if ((dragHandleWidth > 0) && (e.getX() < dragHandleWidth)) {
            dragging = true;
        } else if (viewHandleId != -1) {
            //Find the handle in the list item
            View handleView = itemView.findViewById(viewHandleId);

            if (handleView == null) {
                Log.e(TAG, "The view ID " + viewHandleId + " was not found in the RecycleView item");
                return false;
            }

            //View should be visible to drag
            if (handleView.getVisibility() != View.VISIBLE) {
                return false;
            }

            //We need to find the relative position of the handle to the parent view
            //Then we can work out if the touch is within the handle
            int[] parentItemPos = new int[2];
            itemView.getLocationInWindow(parentItemPos);

            int[] handlePos = new int[2];
            handleView.getLocationInWindow(handlePos);

            int xRel = handlePos[0] - parentItemPos[0];
            int yRel = handlePos[1] - parentItemPos[1];

            Rect touchBounds = new Rect(itemView.getLeft() + xRel, itemView.getTop() + yRel,
                    itemView.getLeft() + xRel + handleView.getWidth(),
                    itemView.getTop() + yRel + handleView.getHeight()
            );

            if (touchBounds.contains((int) e.getX(), (int) e.getY()))
                dragging = true;

            debugLog("parentItemPos = " + parentItemPos[0] + " " + parentItemPos[1]);
            debugLog("handlePos = " + handlePos[0] + " " + handlePos[1]);
        }


        if (dragging) {
            debugLog("Started Drag");

            setIsDragging(true);

            floatingItem = createFloatingBitmap(itemView);

            fingerAnchorY = (int) e.getY();
            fingerOffsetInViewY = fingerAnchorY - itemView.getTop();
            fingerY = fingerAnchorY;

            selectedDragItemPos = rv.getChildLayoutPosition(itemView);
            debugLog("selectedDragItemPos = " + selectedDragItemPos);

            return true;
        }
    }
    return false;
}
 
Example 20
Source File: SpaceItemDecoration.java    From droidkaigi2016 with Apache License 2.0 4 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if (parent.getChildLayoutPosition(view) != 0) {
        outRect.top = space;
    }
}