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

The following examples show how to use android.support.v7.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: ItemTouchHelper.java    From letv with Apache License 2.0 6 votes vote down vote up
public void onMoved(RecyclerView recyclerView, ViewHolder viewHolder, int fromPos, ViewHolder target, int toPos, int x, int y) {
    LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof ViewDropHandler) {
        ((ViewDropHandler) layoutManager).prepareForDrop(viewHolder.itemView, target.itemView, x, y);
        return;
    }
    if (layoutManager.canScrollHorizontally()) {
        if (layoutManager.getDecoratedLeft(target.itemView) <= recyclerView.getPaddingLeft()) {
            recyclerView.scrollToPosition(toPos);
        }
        if (layoutManager.getDecoratedRight(target.itemView) >= recyclerView.getWidth() - recyclerView.getPaddingRight()) {
            recyclerView.scrollToPosition(toPos);
        }
    }
    if (layoutManager.canScrollVertically()) {
        if (layoutManager.getDecoratedTop(target.itemView) <= recyclerView.getPaddingTop()) {
            recyclerView.scrollToPosition(toPos);
        }
        if (layoutManager.getDecoratedBottom(target.itemView) >= recyclerView.getHeight() - recyclerView.getPaddingBottom()) {
            recyclerView.scrollToPosition(toPos);
        }
    }
}
 
Example 2
Source File: HTWrapperAdapter.java    From ht-refreshrecyclerview with MIT License 6 votes vote down vote up
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    mInnerAdapter.onAttachedToRecyclerView(recyclerView);
    super.onAttachedToRecyclerView(recyclerView);
    //对Grid布局进行支持
    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
    if (manager instanceof GridLayoutManager) {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
        final GridLayoutManager.SpanSizeLookup origin = gridLayoutManager.getSpanSizeLookup();
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (isLoadMoreView(position)) {
                    return gridLayoutManager.getSpanCount();
                } else {
                    if (origin != null) {
                        return origin.getSpanSize(position);
                    } else return 1;
                }
            }
        });
    }
}
 
Example 3
Source File: DividerGridItemDecoration.java    From PowerfulRecyclerView with Apache License 2.0 6 votes vote down vote up
private int getSpanCount(RecyclerView parent)
{
    // 列数
    int spanCount = -1;
    LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager)
    {

        spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
    } else if (layoutManager instanceof StaggeredGridLayoutManager)
    {
        spanCount = ((StaggeredGridLayoutManager) layoutManager)
                .getSpanCount();
    }
    return spanCount;
}
 
Example 4
Source File: ScrollLayout.java    From ScrollLayout with Apache License 2.0 6 votes vote down vote up
private void updateRecyclerViewScrollState(RecyclerView recyclerView) {
    if (recyclerView.getChildCount() == 0) {
        setDraggable(true);
    } else {
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        int[] i = new int[1];
        if (layoutManager instanceof LinearLayoutManager || layoutManager instanceof GridLayoutManager) {
            i[0] = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            i = null;
            i = ((StaggeredGridLayoutManager) layoutManager).findFirstVisibleItemPositions(i);
        }
        if (i[0] == 0) {
            View firstChild = recyclerView.getChildAt(0);
            if (firstChild.getTop() == recyclerView.getPaddingTop()) {
                setDraggable(true);
                return;
            }
        }
        setDraggable(false);
    }
}
 
Example 5
Source File: WXRecyclerViewOnScrollListener.java    From weex-uikit with MIT License 6 votes vote down vote up
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  super.onScrollStateChanged(recyclerView, newState);
  currentScrollState = newState;
  RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
  int visibleItemCount = layoutManager.getChildCount();
  int totalItemCount = layoutManager.getItemCount();

  if (visibleItemCount != 0) {
    int bottomOffset = (totalItemCount - lastVisibleItemPosition - 1) * (recyclerView.getHeight()) / visibleItemCount;
    if (visibleItemCount > 0 && currentScrollState == RecyclerView.SCROLL_STATE_IDLE) {
      if (listener != null && listener.get() != null) {
        listener.get().onLoadMore(bottomOffset);
      }
    }
  }
}
 
Example 6
Source File: DividerGridItemDecoration.java    From Pas with Apache License 2.0 6 votes vote down vote up
private boolean isLastColum(RecyclerView parent, int pos, int spanCount,
                            int childCount) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边
        {
            return true;
        }
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        int orientation = ((StaggeredGridLayoutManager) layoutManager)
                .getOrientation();
        if (orientation == StaggeredGridLayoutManager.VERTICAL) {
            if ((pos + 1) % spanCount == 0)// 如果是最后一列,则不需要绘制右边
            {
                return true;
            }
        } else {
            childCount = childCount - childCount % spanCount;
            if (pos >= childCount)// 如果是最后一列,则不需要绘制右边
                return true;
        }
    }
    return false;
}
 
Example 7
Source File: AbstractAdapter.java    From FastDownloader with Apache License 2.0 6 votes vote down vote up
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    mRecyclerView = recyclerView;

    //Grid的 Header Footer 宽度处理
    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) {
                boolean isData = dataSet.data.is(position);
                if (isData) {
                    return 1;
                } else {
                    return gridManager.getSpanCount();
                }
            }
        });
    }
}
 
Example 8
Source File: ScrollPageHelper.java    From YCScrollPager with Apache License 2.0 6 votes vote down vote up
/**
 * 这个方法是与recyclerView绑定
 * @param recyclerView                      recyclerView
 * @throws IllegalStateException
 */
@Override
public void attachToRecyclerView(@Nullable RecyclerView recyclerView)
        throws IllegalStateException {
    if (recyclerView != null && !(recyclerView.getLayoutManager()
            instanceof LinearLayoutManager)) {
        throw new IllegalStateException("ScrollPageHelper needs a " +
                "RecyclerView with a LinearLayoutManager");
    }
    if (recyclerView != null) {
        //设置fling监听为null
        recyclerView.setOnFlingListener(null);
        if ((gravity == Gravity.START || gravity == Gravity.END)) {
            isRtlHorizontal = isRtl();
        }
    }
    super.attachToRecyclerView(recyclerView);
}
 
Example 9
Source File: DividerGridItemDecoration.java    From CoordinatorLayoutExample with Apache License 2.0 5 votes vote down vote up
private int getSpanCount(RecyclerView parent) {
    // 列数
    int spanCount = -1;
    LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {

        spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        spanCount = ((StaggeredGridLayoutManager) layoutManager)
                .getSpanCount();
    }
    return spanCount;
}
 
Example 10
Source File: DividerGridItemDecoration.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * get total Span Count
 *
 * @param parent
 * @return
 */
int getSpanCount(RecyclerView parent) {
    int spanCount = -1;
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
    }
    return spanCount;
}
 
Example 11
Source File: RecyclerViewOnScroll.java    From GankGirl with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);
    currentScrollState = newState;
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    int visibleItemCount = layoutManager.getChildCount();
    int totalItemCount = layoutManager.getItemCount();
    if ((visibleItemCount > 0 && currentScrollState == RecyclerView.SCROLL_STATE_IDLE && (lastVisibleItemPosition) >= totalItemCount - 1) && mSuperRefreshLayout.isLoadMore()) {
        mSuperRefreshLayout.loadMore();
    }
}
 
Example 12
Source File: BaseDecoration.java    From AndroidFrame 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 pos = parent.getChildAdapterPosition(view);
    RecyclerView.LayoutManager manager = parent.getLayoutManager();
    if (manager instanceof GridLayoutManager) {
        //网格布局
        int spanCount = ((GridLayoutManager) manager).getSpanCount();
        if (isFirstLineInGroup(pos, spanCount)) {
            //为悬浮view预留空间
            outRect.top = mGroupHeight;
        } else {
            //为分割线预留空间
            outRect.top = mDivideHeight;
        }
    } else {
        //其他的默认为线性布局
        //只有是同一组的第一个才显示悬浮栏
        if (isFirstInGroup(pos)) {
            //为悬浮view预留空间
            outRect.top = mGroupHeight;
        } else {
            //为分割线预留空间
            outRect.top = mDivideHeight;
        }
    }
}
 
Example 13
Source File: DividerItemDecoration.java    From ExpandableRecyclerview-Databinding with Apache License 2.0 5 votes vote down vote up
protected LinearLayoutManager getLinearLayoutManger(RecyclerView parent) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof LinearLayoutManager) {
        return (LinearLayoutManager) layoutManager;
    }
    return null;
}
 
Example 14
Source File: RecyclerViewScrollListener.java    From Social with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

    //RecycleView 显示的条目数
    int visibleCount = layoutManager.getChildCount();
    Log.d(TAG, "onScrollStateChanged: visibleCount = " + visibleCount);

    //显示数据总数
    int totalCount = layoutManager.getItemCount();
    Log.d(TAG, "onScrollStateChanged: totalCount = " + totalCount);

    Log.d(TAG, "onScrollStateChanged: lastVisibleItemPosition = " + mLastVisibleItemPosition);

    // 四个条件,分别是是否有数据,状态是否是滑动停止状态,显示的最大条目是否大于整个数据(注意偏移量),是否正在加载数据
    if(visibleCount>0
            &&newState==RecyclerView.SCROLL_STATE_IDLE
            &&mLastVisibleItemPosition>=totalCount-1
            &&!isLoadData){
        //可以加载数据
        if(mListener!=null){
            isLoadData = true;
            mListener.loadMore();
        }
    }else{
        Log.d(TAG, "onScrollStateChanged: no data");
    }

}
 
Example 15
Source File: BaseDivider.java    From TitanRecyclerView with MIT License 5 votes vote down vote up
/**
 * Determines whether divider was already drawn for the row the item is in,
 * effectively only makes sense for a grid
 *
 * @param position current view position to draw divider
 * @param parent   RecyclerView
 * @return true if the divider can be skipped as it is in the same row as the previous one.
 */
private boolean wasDividerAlreadyDrawn(int position, RecyclerView parent) {
    if (parent.getLayoutManager() instanceof GridLayoutManager) {
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        GridLayoutManager.SpanSizeLookup spanSizeLookup = layoutManager.getSpanSizeLookup();
        int spanCount = layoutManager.getSpanCount();
        return spanSizeLookup.getSpanIndex(position, spanCount) > 0;
    }

    return false;
}
 
Example 16
Source File: X8LocalFragmentPresenter.java    From FimiX8-RE with MIT License 5 votes vote down vote up
public X8LocalFragmentPresenter(RecyclerView mRecyclerView, X8sPanelRecycleAdapter mPanelRecycleAdapter, ISelectData mISelectData, Context context) {
    super(mRecyclerView, mPanelRecycleAdapter, mISelectData, context, false);
    doTrans();
    LayoutManager manager = mRecyclerView.getLayoutManager();
    if (manager instanceof GridLayoutManager) {
        this.mGridLayoutManager = (GridLayoutManager) manager;
    }
    registerReciver();
}
 
Example 17
Source File: DefineActivity.java    From SwipeRecyclerView-master with Apache License 2.0 5 votes vote down vote up
@Override
public int onDragFlags(RecyclerView recyclerView, RecyclerView.ViewHolder targetViewHolder) {
    int adapterPosition = targetViewHolder.getAdapterPosition();
    if (adapterPosition == 0) { // 这里让HeaderView不能拖拽。
        return OnItemMovementListener.INVALID;// 返回无效的方向。
    }

    // 真实的Position:通过ViewHolder拿到的position都需要减掉HeadView的数量。
    int position = adapterPosition - mRecyclerView.getHeaderItemCount();

    // 假如让普通Item的第一个不能拖拽。
    if (position == 0) {
        return OnItemMovementListener.INVALID;// 返回无效的方向。
    }

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        // Grid可以上下左右拖拽。
        return OnItemMovementListener.LEFT |
                OnItemMovementListener.UP |
                OnItemMovementListener.RIGHT |
                OnItemMovementListener.DOWN;
    } else if (layoutManager instanceof LinearLayoutManager) {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;

        // 横向List只能左右拖拽。
        if (linearLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
            return (OnItemMovementListener.LEFT | OnItemMovementListener.RIGHT);
        }
        // 竖向List只能上下拖拽。
        else {
            return OnItemMovementListener.UP | OnItemMovementListener.DOWN;
        }
    }
    return OnItemMovementListener.INVALID;// 返回无效的方向。
}
 
Example 18
Source File: PinnedHeaderItemDecoration.java    From PinnedSectionItemDecoration with Apache License 2.0 4 votes vote down vote up
@Override
public void getItemOffsets(final Rect outRect, final View view, final RecyclerView parent, RecyclerView.State state) {

    checkCache(parent);

    if (!mEnableDivider) {
        return;
    }

    if (mDrawable == null) {
        mDrawable = ContextCompat.getDrawable(parent.getContext(), mDividerId != 0 ? mDividerId : R.drawable.divider);
    }

    if (parent.getLayoutManager() instanceof GridLayoutManager) {
        if (!isPinnedHeader(parent, view)) {
            final int spanCount = getSpanCount(parent);
            int position = parent.getChildAdapterPosition(view);
            if (isFirstColumn(parent, position, spanCount)) {
                // 第一列要多画左边
                outRect.set(mDrawable.getIntrinsicWidth(), 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
            } else {
                outRect.set(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
            }
        } else {
            // 标签画底部分隔线
            outRect.set(0, 0, 0, mDrawable.getIntrinsicHeight());
        }
    } else if (parent.getLayoutManager() instanceof LinearLayoutManager) {
        outRect.set(0, 0, 0, mDrawable.getIntrinsicHeight());
    } else if (parent.getLayoutManager() instanceof StaggeredGridLayoutManager) {
        if (isPinnedHeader(parent, view)) {
            outRect.set(0, 0, 0, mDrawable.getIntrinsicHeight());
        } else {
            final StaggeredGridLayoutManager.LayoutParams slp = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
            // slp.getSpanIndex(): 这个可以拿到它在同一行排序的真实顺序
            if (slp.getSpanIndex() == 0) {
                outRect.set(mDrawable.getIntrinsicWidth(), 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
            } else {
                outRect.set(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
            }
        }
    }
}
 
Example 19
Source File: LoadMoreScrollListener.java    From LazyRecyclerAdapter with MIT License 4 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManagerType == null) {
        if (layoutManager instanceof LinearLayoutManager) {
            layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR;
        } else if (layoutManager instanceof GridLayoutManager) {
            layoutManagerType = LAYOUT_MANAGER_TYPE.GRID;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID;
        } else {
            throw new RuntimeException("Unsupported");
        }
    }

    switch (layoutManagerType) {
        case LINEAR:
            lastVisibleItemPosition = ((LinearLayoutManager) layoutManager)
                    .findLastVisibleItemPosition();

            firstVisibleItemPosition = ((LinearLayoutManager) layoutManager)
                    .findFirstVisibleItemPosition();
            break;
        case GRID:
            lastVisibleItemPosition = ((GridLayoutManager) layoutManager)
                    .findLastVisibleItemPosition();
            firstVisibleItemPosition = ((GridLayoutManager) layoutManager)
                    .findFirstVisibleItemPosition();
            break;
        case STAGGERED_GRID:
            StaggeredGridLayoutManager staggeredGridLayoutManager
                    = (StaggeredGridLayoutManager) layoutManager;
            if (lastPositions == null) {
                lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            }
            if (firstPositions == null) {
                firstPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            }
            staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
            staggeredGridLayoutManager.findFirstVisibleItemPositions(firstPositions);
            lastVisibleItemPosition = findMax(lastPositions);
            firstVisibleItemPosition = findMin(firstPositions);
            break;
    }
    onScrolled(firstVisibleItemPosition);


}
 
Example 20
Source File: DividerItemDecoration.java    From aptoide-client with GNU General Public License v2.0 4 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;

    // Add top margin only for the first item to avoid double space between items

    if(parent.getLayoutManager() instanceof GridLayoutManager){

        int colcount = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();

        if(parent.getChildPosition(view) < colcount){
            outRect.top = space;
        }

    }else if(parent.getChildPosition(view) == 0){
        outRect.top = space;
    }



}