Java Code Examples for androidx.recyclerview.widget.RecyclerView#getLayoutManager()

The following examples show how to use androidx.recyclerview.widget.RecyclerView#getLayoutManager() . 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: HorizontalScrollCompat.java    From SmoothRefreshLayout with MIT License 6 votes vote down vote up
public static boolean isScrollingView(View view) {
    if (ViewCatcherUtil.isViewPager(view)
            || view instanceof HorizontalScrollView
            || view instanceof WebView) {
        return true;
    } else if (ViewCatcherUtil.isRecyclerView(view)) {
        RecyclerView recyclerView = (RecyclerView) view;
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager != null) {
            if (manager instanceof LinearLayoutManager) {
                LinearLayoutManager linearManager = ((LinearLayoutManager) manager);
                return linearManager.getOrientation() == RecyclerView.HORIZONTAL;
            } else if (manager instanceof StaggeredGridLayoutManager) {
                StaggeredGridLayoutManager gridLayoutManager =
                        (StaggeredGridLayoutManager) manager;
                return gridLayoutManager.getOrientation() == RecyclerView.HORIZONTAL;
            }
        }
    }
    return false;
}
 
Example 2
Source File: BackToTopUtils.java    From Mysplash with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void scrollToTop(RecyclerView recyclerView) {
    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
    int firstVisibleItemPosition = 0;
    if (manager instanceof LinearLayoutManager) {
        firstVisibleItemPosition = getFirstVisibleItemPosition((LinearLayoutManager) manager);
    } else if (manager instanceof StaggeredGridLayoutManager) {
        firstVisibleItemPosition = getFirstVisibleItemPosition((StaggeredGridLayoutManager) manager);
    }
    if (firstVisibleItemPosition > 5) {
        recyclerView.scrollToPosition(5);
    }
    recyclerView.smoothScrollToPosition(0);

    MysplashActivity activity = MysplashApplication.getInstance().getTopActivity();
    if (activity != null) {
        ComponentFactory.getSettingsService().notifySetBackToTop(activity);
    }
}
 
Example 3
Source File: RcvSectionMultiLabelAdapter.java    From RecyclerViewAdapter with Apache License 2.0 6 votes vote down vote up
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView)
{
    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
    if (manager instanceof GridLayoutManager)
    {
        final GridLayoutManager gridManager = ((GridLayoutManager) manager);
        gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
        {
            @Override
            public int getSpanSize(int position)
            {
                if (isInHeadViewPos(position)
                        || isInSectionLabelPos(position)
                        || isInFootViewPos(position)
                        || isInLoadMorePos(position)
                        || isInEmptyStatus())
                    return gridManager.getSpanCount();
                else
                    return 1;
            }
        });
        gridManager.setSpanCount(gridManager.getSpanCount());
    }
}
 
Example 4
Source File: GravityDelegate.java    From GetApk with MIT License 5 votes vote down vote up
int getSnappedPosition(RecyclerView recyclerView) {
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

    if (layoutManager instanceof LinearLayoutManager) {
        if (gravity == Gravity.START || gravity == Gravity.TOP) {
            return ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
        } else if (gravity == Gravity.END || gravity == Gravity.BOTTOM) {
            return ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition();
        }
    }

    return RecyclerView.NO_POSITION;
}
 
Example 5
Source File: CustomDividerItemDecoration.java    From Android-skin-support with MIT License 5 votes vote down vote up
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    if (parent.getLayoutManager() == null) {
        return;
    }
    if (mOrientation == VERTICAL) {
        drawVertical(c, parent);
    } else {
        drawHorizontal(c, parent);
    }
}
 
Example 6
Source File: ItemTouchHelperExtension.java    From CrazyDaily with Apache License 2.0 5 votes vote down vote up
/**
 * Called when {@link #onMove(RecyclerView, ViewHolder, ViewHolder)} returns true.
 * <p>
 * ItemTouchHelper does not create an extra Bitmap or View while dragging, instead, it
 * modifies the existing View. Because of this reason, it is important that the View is
 * still part of the layout after it is moved. This may not work as intended when swapped
 * Views are close to RecyclerView bounds or there are gaps between them (e.g. other Views
 * which were not eligible for dropping over).
 * <p>
 * This method is responsible to give necessary hint to the LayoutManager so that it will
 * keep the View in visible area. For example, for LinearLayoutManager, this is as simple
 * <p>
 * <p>
 * Default implementation calls {@link RecyclerView#scrollToPosition(int)} if the View's
 * new position is likely to be out of bounds.
 * <p>
 * It is important to ensure the ViewHolder will stay visible as otherwise, it might be
 * removed by the LayoutManager if the move causes the View to go out of bounds. In that
 * case, drag will end prematurely.
 *
 * @param recyclerView The RecyclerView controlled by the ItemTouchHelper.
 * @param viewHolder   The ViewHolder under user's control.
 * @param fromPos      The previous adapter position of the dragged item (before it was
 *                     moved).
 * @param target       The ViewHolder on which the currently active item has been dropped.
 * @param toPos        The new adapter position of the dragged item.
 * @param x            The updated left value of the dragged View after drag translations
 *                     are applied. This value does not include margins added by
 *                     {@link RecyclerView.ItemDecoration}s.
 * @param y            The updated top value of the dragged View after drag translations
 *                     are applied. This value does not include margins added by
 *                     {@link RecyclerView.ItemDecoration}s.
 */
public void onMoved(final RecyclerView recyclerView,
                    final ViewHolder viewHolder, int fromPos, final ViewHolder target, int toPos, int x,
                    int y) {
    final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof ViewDropHandler) {
        ((ViewDropHandler) layoutManager).prepareForDrop(viewHolder.itemView,
                target.itemView, x, y);
        return;
    }

    // if layout manager cannot handle it, do some guesswork
    if (layoutManager.canScrollHorizontally()) {
        final int minLeft = layoutManager.getDecoratedLeft(target.itemView);
        if (minLeft <= recyclerView.getPaddingLeft()) {
            recyclerView.scrollToPosition(toPos);
        }
        final int maxRight = layoutManager.getDecoratedRight(target.itemView);
        if (maxRight >= recyclerView.getWidth() - recyclerView.getPaddingRight()) {
            recyclerView.scrollToPosition(toPos);
        }
    }

    if (layoutManager.canScrollVertically()) {
        final int minTop = layoutManager.getDecoratedTop(target.itemView);
        if (minTop <= recyclerView.getPaddingTop()) {
            recyclerView.scrollToPosition(toPos);
        }
        final int maxBottom = layoutManager.getDecoratedBottom(target.itemView);
        if (maxBottom >= recyclerView.getHeight() - recyclerView.getPaddingBottom()) {
            recyclerView.scrollToPosition(toPos);
        }
    }
}
 
Example 7
Source File: StickyViewBehavior.java    From brickkit-android with Apache License 2.0 5 votes vote down vote up
/**
 * Static helper method to get the orientation of a recycler view.
 *
 * @param recyclerView {@link RecyclerView} whose orientation we are getting
 * @return the orientation of the given RecyclerView if it can be found, LinearLayoutManager.HORIZONTAL otherwise
 */
static int getOrientation(RecyclerView recyclerView) {
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof LinearLayoutManager) {
        return ((LinearLayoutManager) layoutManager).getOrientation();
    }
    return LinearLayoutManager.HORIZONTAL;
}
 
Example 8
Source File: EventItemDecorator.java    From mage-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    if (parent.getLayoutManager() == null) {
        return;
    }

    c.save();
    final int left;
    final int right;
    if (parent.getClipToPadding()) {
        left = parent.getPaddingLeft();
        right = parent.getWidth() - parent.getPaddingRight();
        c.clipRect(left, parent.getPaddingTop(), right, parent.getHeight() - parent.getPaddingBottom());
    } else {
        left = 0;
        right = parent.getWidth();
    }

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        parent.getDecoratedBoundsWithMargins(child, bounds);

        int position = parent.getChildAdapterPosition(child);
        if (parent.getAdapter().getItemViewType(position) == EventListAdapter.ITEM_TYPE_HEADER ||
            parent.getAdapter().getItemViewType(position + 1) == EventListAdapter.ITEM_TYPE_HEADER ||
            position == state.getItemCount() - 1) {
            continue;
        }

        final int bottom = bounds.bottom + Math.round(ViewCompat.getTranslationY(child));
        final int top = bottom - divider.getIntrinsicHeight();
        divider.setBounds(left, top, right, bottom);
        divider.draw(c);
    }
    c.restore();
}
 
Example 9
Source File: SearchResultFragment.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
@Override public void scrollToTop() {
  RecyclerView list;
  if (followedStoresResultList.getVisibility() == View.VISIBLE) {
    list = followedStoresResultList;
  } else {
    list = allStoresResultList;
  }
  LinearLayoutManager layoutManager = ((LinearLayoutManager) list.getLayoutManager());
  int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
  if (lastVisibleItemPosition > 10) {
    list.scrollToPosition(10);
  }
  list.smoothScrollToPosition(0);
}
 
Example 10
Source File: GalleryLayoutManager.java    From CardSlideView with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);
    state = newState;
    if (state == RecyclerView.SCROLL_STATE_DRAGGING) {
        isDrag = true;
    }
    if (state == RecyclerView.SCROLL_STATE_IDLE) {
        isDrag = false;
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager == null) {
            return;
        }
        if (mSnapHelper == null) {
            return;
        }
        View snap = mSnapHelper.findSnapView(layoutManager);
        if (snap == null) {
            return;
        }
        int selectedPosition = layoutManager.getPosition(snap);
        if (mOnPageChangeListener != null && selectedPosition != mCurItem) {
            mCurItem = selectedPosition;
            mOnPageChangeListener.onPageSelected(mCurItem);
        }
    }
}
 
Example 11
Source File: ScrollPositionListener.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    LinearLayoutManager llm = (LinearLayoutManager) recyclerView.getLayoutManager();
    if (llm != null) {
        int firstVisibleItemPosition = llm.findFirstVisibleItemPosition();
        int lastCompletelyVisibleItemPosition = llm.findLastCompletelyVisibleItemPosition();
        onPositionChangeListener.accept(Math.max(firstVisibleItemPosition, lastCompletelyVisibleItemPosition));
    }
}
 
Example 12
Source File: DividerItemDecoration.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    if (parent.getLayoutManager() != null && this.mDivider != null) {
        if (this.mOrientation == 1) {
            this.drawVertical(c, parent);
        } else {
            this.drawHorizontal(c, parent);
        }
    }
}
 
Example 13
Source File: DividerGridItemDecoration.java    From cv4j with Apache License 2.0 5 votes vote down vote up
/**
 * 是否是最后一行
 */
private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount)
{
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager)
    {
        childCount = childCount - childCount % spanCount;
        if (pos >= childCount)// 如果是最后一行,则不需要绘制底部
            return true;
    } else if (layoutManager instanceof StaggeredGridLayoutManager)
    {
        int orientation = ((StaggeredGridLayoutManager) layoutManager)
                .getOrientation();
        // StaggeredGridLayoutManager 且纵向滚动
        if (orientation == StaggeredGridLayoutManager.VERTICAL)
        {
            childCount = childCount - childCount % spanCount;
            // 如果是最后一行,则不需要绘制底部
            if (pos >= childCount)
                return true;
        } else
        // StaggeredGridLayoutManager 且横向滚动
        {
            // 如果是最后一行,则不需要绘制底部
            if ((pos + 1) % spanCount == 0)
            {
                return true;
            }
        }
    }
    return false;
}
 
Example 14
Source File: GreedoSpacingItemDecoration.java    From greedo-layout-for-android with MIT License 5 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if (!(parent.getLayoutManager() instanceof GreedoLayoutManager)) {
        throw new IllegalArgumentException(String.format("The %s must be used with a %s",
                GreedoSpacingItemDecoration.class.getSimpleName(),
                GreedoLayoutManager.class.getSimpleName()));
    }

    final GreedoLayoutManager layoutManager = (GreedoLayoutManager) parent.getLayoutManager();

    int childIndex = parent.getChildAdapterPosition(view);
    if (childIndex == RecyclerView.NO_POSITION) return;

    outRect.top    = 0;
    outRect.bottom = mSpacing;
    outRect.left   = 0;
    outRect.right  = mSpacing;

    // Add inter-item spacings
    if (isTopChild(childIndex, layoutManager)) {
        outRect.top = mSpacing;
    }

    if (isLeftChild(childIndex, layoutManager)) {
        outRect.left = mSpacing;
    }
}
 
Example 15
Source File: DividerGridItemDecorationUtils.java    From shinny-futures-android with GNU General Public License v3.0 5 votes vote down vote up
private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
                          int childCount) {
    LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        childCount = childCount - childCount % spanCount;
        if (pos >= childCount)// 如果是最后一行,则不需要绘制底部
            return true;
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        int orientation = ((StaggeredGridLayoutManager) layoutManager)
                .getOrientation();
        // StaggeredGridLayoutManager 且纵向滚动
        if (orientation == StaggeredGridLayoutManager.VERTICAL) {
            childCount = childCount - childCount % spanCount;
            // 如果是最后一行,则不需要绘制底部
            if (pos >= childCount)
                return true;
        } else
        // StaggeredGridLayoutManager 且横向滚动
        {
            // 如果是最后一行,则不需要绘制底部
            if ((pos + 1) % spanCount == 0) {
                return true;
            }
        }
    }
    return false;
}
 
Example 16
Source File: GridSectionAverageGapItemDecoration.java    From BaseRecyclerViewAdapterHelper with MIT License 4 votes vote down vote up
@Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (parent.getLayoutManager() instanceof GridLayoutManager && parent.getAdapter() instanceof BaseSectionQuickAdapter) {
            GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
            BaseSectionQuickAdapter<SectionEntity, BaseViewHolder> adapter = (BaseSectionQuickAdapter) parent.getAdapter();
            if (mAdapter != adapter) {
                setUpWithAdapter(adapter);
            }
            int spanCount = layoutManager.getSpanCount();
            int position = parent.getChildAdapterPosition(view) - mAdapter.getHeaderLayoutCount();
            SectionEntity entity = adapter.getItem(position);

            if (entity == null || entity.isHeader()) {
                //不处理header
                outRect.set(0, 0, 0, 0);
//                Log.w("GridAverageGapItem", "pos=" + position + "," + outRect.toShortString());
                return;
            }

            Section section = findSectionLastItemPos(position);

            if (gapHSizePx < 0 || gapVSizePx < 0) {
                transformGapDefinition(parent, spanCount);
            }
            outRect.top = gapVSizePx;
            outRect.bottom = 0;

            //下面的visualPos为单个Section内的视觉Pos
            int visualPos = position + 1 - section.startPos;
            if (visualPos % spanCount == 1) {
                //第一列
                outRect.left = sectionEdgeHPaddingPx;
                outRect.right = eachItemHPaddingPx - sectionEdgeHPaddingPx;
            } else if (visualPos % spanCount == 0) {
                //最后一列
                outRect.left = eachItemHPaddingPx - sectionEdgeHPaddingPx;
                outRect.right = sectionEdgeHPaddingPx;
            } else {
                outRect.left = gapHSizePx - (eachItemHPaddingPx - sectionEdgeHPaddingPx);
                outRect.right = eachItemHPaddingPx - outRect.left;
            }

            if (visualPos - spanCount <= 0) {
                //第一行
                outRect.top = sectionEdgeVPaddingPx;
            }

            if (isLastRow(visualPos, spanCount, section.getCount())) {
                //最后一行
                outRect.bottom = sectionEdgeVPaddingPx;
//                Log.w("GridAverageGapItem", "last row pos=" + position);
            }
//            Log.w("GridAverageGapItem", "pos=" + position + ",vPos=" + visualPos + "," + outRect.toShortString());
        } else {
            super.getItemOffsets(outRect, view, parent, state);
        }
    }
 
Example 17
Source File: EndlessRecyclerViewScrollListener.java    From arcusandroid with Apache License 2.0 4 votes vote down vote up
public EndlessRecyclerViewScrollListener(RecyclerView recyclerView) {
    this.recyclerView = recyclerView;
    linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
}
 
Example 18
Source File: StickyItemDecoration.java    From AndroidAnimationExercise with Apache License 2.0 4 votes vote down vote up
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(c, parent, state);

    if (parent.getAdapter().getItemCount() <= 0) return;

    mLayoutManager = (LinearLayoutManager) parent.getLayoutManager();
    mCurrentUIFindStickView = false;

    for (int m = 0, size = parent.getChildCount(); m < size; m++) {
        View view = parent.getChildAt(m);

        /**
         * 如果是吸附的view
         */
        if (mStickyView.isStickyView(view)) {
            mCurrentUIFindStickView = true;
            getStickyViewHolder(parent);
            cacheStickyViewPosition(m);

            if (view.getTop() <= 0) {
                bindDataForStickyView(mLayoutManager.findFirstVisibleItemPosition(), parent.getMeasuredWidth());
            } else {
                if (mStickyPositionList.size() > 0) {
                    if (mStickyPositionList.size() == 1) {
                        bindDataForStickyView(mStickyPositionList.get(0), parent.getMeasuredWidth());
                    } else {
                        int currentPosition = getStickyViewPositionOfRecyclerView(m);
                        int indexOfCurrentPosition = mStickyPositionList.lastIndexOf(currentPosition);
                        if (indexOfCurrentPosition >= 1) bindDataForStickyView(mStickyPositionList.get(indexOfCurrentPosition - 1), parent.getMeasuredWidth());
                    }
                }
            }

            if (view.getTop() > 0 && view.getTop() <= mStickyItemViewHeight) {
                mStickyItemViewMarginTop = mStickyItemViewHeight - view.getTop();
            } else {
                mStickyItemViewMarginTop = 0;

                View nextStickyView = getNextStickyView(parent);
                if (nextStickyView != null && nextStickyView.getTop() <= mStickyItemViewHeight) {
                    mStickyItemViewMarginTop = mStickyItemViewHeight - nextStickyView.getTop();
                }

            }

            drawStickyItemView(c);
            break;
        }
    }

    if (!mCurrentUIFindStickView) {
        mStickyItemViewMarginTop = 0;
        if (mLayoutManager.findFirstVisibleItemPosition() + parent.getChildCount() == parent.getAdapter().getItemCount() && mStickyPositionList.size() > 0) {
            bindDataForStickyView(mStickyPositionList.get(mStickyPositionList.size() - 1), parent.getMeasuredWidth());
        }
        drawStickyItemView(c);
    }
}
 
Example 19
Source File: FollowingItemDecoration.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
                           @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);

    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager == null) {
        return;
    }

    FollowingAdapter adapter = (FollowingAdapter) parent.getAdapter();
    if (adapter == null) {
        return;
    }

    parentPaddingLeft = parent.getPaddingLeft();
    parentPaddingRight = parent.getPaddingRight();
    parentPaddingTop = parent.getPaddingTop();
    parentPaddingBottom = parent.getPaddingBottom();
    insets = getWindowInset(parent);
    if (parentPaddingLeft != insets.left
            || parentPaddingRight != insets.right
            || parentPaddingTop != 0
            || parentPaddingBottom != insets.bottom) {
        setParentPadding(parent, insets);
    }

    StaggeredGridLayoutManager.LayoutParams params
            = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();

    int spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
    int spanIndex = params.getSpanIndex();

    if (adapter.isPhotoItem(params.getViewAdapterPosition())) {
        if (spanCount == 1) {
            outRect.set(
                    marginSingleSpanPhoto.left,
                    0,
                    marginSingleSpanPhoto.right,
                    marginSingleSpanPhoto.bottom
            );
        } else {
            if (spanIndex == 0) {
                outRect.set(marginGridItem, 0, marginGridItem, marginGridItem);
            } else {
                outRect.set(0, 0, marginGridItem, marginGridItem);
            }
        }
    }
}
 
Example 20
Source File: DynamicLayoutUtils.java    From dynamic-support with Apache License 2.0 3 votes vote down vote up
/**
 * Sets full span for the header and empty view in case of a {@link GridLayoutManager}.
 * This method must be called after setting an adapter for the recycler view.
 *
 * @param recyclerView The recycler view to set the span size.
 *
 * @see DynamicRecyclerViewAdapter.ItemType
 * @see GridLayoutManager#setSpanSizeLookup(GridLayoutManager.SpanSizeLookup)
 * @see #setFullSpanForType(RecyclerView, Integer[], int)
 *
 * @see DynamicRecyclerViewAdapter#TYPE_SECTION_HEADER
 * @see DynamicRecyclerViewAdapter#TYPE_EMPTY_VIEW
 */
public static void setFullSpanForType(@Nullable final RecyclerView recyclerView) {
    if (recyclerView == null || recyclerView.getAdapter() == null) {
        return;
    }

    if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
        setFullSpanForType(recyclerView, new Integer[] {
                DynamicRecyclerViewAdapter.TYPE_SECTION_HEADER,
                        DynamicRecyclerViewAdapter.TYPE_EMPTY_VIEW },
                ((GridLayoutManager) recyclerView.getLayoutManager()).getSpanCount());
    }
}