Java Code Examples for android.support.v7.widget.GridLayoutManager#setSpanCount()

The following examples show how to use android.support.v7.widget.GridLayoutManager#setSpanCount() . 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: HeaderAndFooterWrapper.java    From styT with Apache License 2.0 6 votes vote down vote up
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    //为了兼容GridLayout
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (isHeaderViewPos(position)) {
                    return gridLayoutManager.getSpanCount();
                } else if (isFooterViewPos(position)) {
                    return gridLayoutManager.getSpanCount();
                }
                if (spanSizeLookup != null)
                    return spanSizeLookup.getSpanSize(position);
                return 1;
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }

}
 
Example 2
Source File: WrapperUtils.java    From CoordinatorLayoutExample with Apache License 2.0 6 votes vote down vote up
public static void onAttachedToRecyclerView(RecyclerView.Adapter innerAdapter, RecyclerView
        recyclerView, final SpanSizeCallback callback) {
    innerAdapter.onAttachedToRecyclerView(recyclerView);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager
                .getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return callback.getSpanSize(gridLayoutManager, spanSizeLookup, position);
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }
}
 
Example 3
Source File: HeaderAndFooterAdapterWrapper.java    From NoListAdapter with Apache License 2.0 6 votes vote down vote up
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    mAdapter.onAttachedToRecyclerView(recyclerView);
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        final GridLayoutManager manager = (GridLayoutManager) layoutManager;
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return (isHeaderViewPos(position) || isFooterViewPos(position)) ?
                        manager.getSpanCount() : 1;
            }
        });
        manager.setSpanCount(manager.getSpanCount());
    }
}
 
Example 4
Source File: GridRecyclerFragment.java    From aptoide-client with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void setLayoutManager(final RecyclerView recyclerView) {
    final GridLayoutManager gridLayoutManager = new GridLayoutManager(recyclerView.getContext(), getColumnSize() * 2);
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {

            if(!(recyclerView.getAdapter() instanceof SpannableRecyclerAdapter)){
                throw new IllegalStateException("RecyclerView adapter must implement SpannableRecyclerAdapter");
            }

            return AptoideUtils.UI.getSpanSize(recyclerView, position);
        }
    });

    // we need to force the spanCount, or it will crash.
    // https://code.google.com/p/android/issues/detail?id=182400
    gridLayoutManager.setSpanCount(getColumnSize()* 2);
    recyclerView.setLayoutManager(gridLayoutManager);
}
 
Example 5
Source File: CursorLoaderGridRecyclerFragment.java    From aptoide-client with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void setLayoutManager(final RecyclerView recyclerView) {
    final GridLayoutManager gridLayoutManager = new GridLayoutManager(recyclerView.getContext(), getColumnSize() * getColumnMultiplier());
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {

            if(!(recyclerView.getAdapter() instanceof SpannableRecyclerAdapter)){
                throw new IllegalStateException("RecyclerView adapter must extend SpannableRecyclerAdapter");
            }

            return AptoideUtils.UI.getSpanSize(recyclerView, position);
        }
    });

    gridLayoutManager.setSpanCount(getColumnSize() * getColumnMultiplier());
    recyclerView.setLayoutManager(gridLayoutManager);
}
 
Example 6
Source File: WrapperUtils.java    From baseAdapter with Apache License 2.0 6 votes vote down vote up
public static void onAttachedToRecyclerView(RecyclerView.Adapter innerAdapter, RecyclerView recyclerView, final SpanSizeCallback callback)
{
    innerAdapter.onAttachedToRecyclerView(recyclerView);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager)
    {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
        {
            @Override
            public int getSpanSize(int position)
            {
                return callback.getSpanSize(gridLayoutManager, spanSizeLookup, position);
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }
}
 
Example 7
Source File: ApplyFragment.java    From candybar-library with Apache License 2.0 6 votes vote down vote up
private void resetSpanSizeLookUp() {
    int column = getActivity().getResources().getInteger(R.integer.apply_column_count);
    LauncherAdapter adapter = (LauncherAdapter) mRecyclerView.getAdapter();
    GridLayoutManager manager = (GridLayoutManager) mRecyclerView.getLayoutManager();

    try {
        manager.setSpanCount(column);

        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == adapter.getFirstHeaderPosition() || position == adapter.getLastHeaderPosition())
                    return column;
                return 1;
            }
        });
    } catch (Exception ignored) {}
}
 
Example 8
Source File: RcvMutilAdapter.java    From FamilyChat with Apache License 2.0 6 votes vote down vote up
/**
 * 添加HeadView或FootView或LoadMore或EmptyView
 * 兼容GridLayoutMananger的方法
 */
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView)
{
    super.onAttachedToRecyclerView(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)
                        || isInFootViewPos(position)
                        || isInLoadMorePos(position)
                        || isInEmptyStatus())
                    return gridManager.getSpanCount();
                else
                    return 1;
            }
        });
        gridManager.setSpanCount(gridManager.getSpanCount());
    }
}
 
Example 9
Source File: RcvSectionAdapter.java    From FamilyChat 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 (isInSectionPos(position)
                        || isInHeadViewPos(position)
                        || isInFootViewPos(position)
                        || isInLoadMorePos(position)
                        || isInEmptyStatus())
                    return gridManager.getSpanCount();
                else
                    return 1;
            }
        });
        gridManager.setSpanCount(gridManager.getSpanCount());
    }
}
 
Example 10
Source File: WrapperUtils.java    From Bailan with Apache License 2.0 6 votes vote down vote up
public static void onAttachedToRecyclerView(RecyclerView.Adapter innerAdapter, RecyclerView recyclerView, final SpanSizeCallback callback) {
    innerAdapter.onAttachedToRecyclerView(recyclerView);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return callback.getSpanSize(gridLayoutManager, spanSizeLookup, position);
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }
}
 
Example 11
Source File: WrapperUtils.java    From MVVM-JueJin with MIT License 6 votes vote down vote up
public static void onAttachedToRecyclerView(RecyclerView.Adapter innerAdapter, RecyclerView recyclerView, final SpanSizeCallback callback)
{
    innerAdapter.onAttachedToRecyclerView(recyclerView);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager)
    {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
        {
            @Override
            public int getSpanSize(int position)
            {
                return callback.getSpanSize(gridLayoutManager, spanSizeLookup, position);
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }
}
 
Example 12
Source File: WrapperUtils.java    From PocketEOS-Android with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void onAttachedToRecyclerView(RecyclerView.Adapter innerAdapter, RecyclerView recyclerView, final SpanSizeCallback callback)
{
    innerAdapter.onAttachedToRecyclerView(recyclerView);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager)
    {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
        {
            @Override
            public int getSpanSize(int position)
            {
                return callback.getSpanSize(gridLayoutManager, spanSizeLookup, position);
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }
}
 
Example 13
Source File: BaseAdapter.java    From MvpRoute with Apache License 2.0 6 votes vote down vote up
/**
 * 解决GrildLayoutManager问题
 *
 * @param recyclerView
 */
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
	super.onAttachedToRecyclerView(recyclerView);
	RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
	if (layoutManager instanceof GridLayoutManager) {
		final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
		final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

		gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
			@Override
			public int getSpanSize(int position) {
				if (isHeaderPos(position)) {
					return gridLayoutManager.getSpanCount();
				} else if (isFooterPos(position)) {
					return gridLayoutManager.getSpanCount();
				}
				if (spanSizeLookup != null) {
					return spanSizeLookup.getSpanSize(position);
				}
				return 1;
			}
		});
		gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
	}
}
 
Example 14
Source File: CommonAdapter.java    From videocreator with Apache License 2.0 6 votes vote down vote up
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
    if (mSupport == null || recyclerView == null) {
        return;
    }
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();
        // 如果设置合并单元格就占用SpanCount那个多个位置
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (mSupport.isSpan(data.get(position))) {
                    return gridLayoutManager.getSpanCount();
                } else if (spanSizeLookup != null) {
                    return spanSizeLookup.getSpanSize(position);
                }
                return 1;
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }
}
 
Example 15
Source File: HelperAdapter.java    From Android-RecyclerViewHelper with Apache License 2.0 5 votes vote down vote up
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);

    itemAdapter.onAttachedToRecyclerView(recyclerView);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int viewType = getItemViewType(position);

                if (ViewType.TYPE_HEADER == viewType) {
                    return gridLayoutManager.getSpanCount();
                } else if (ViewType.TYPE_FOOTER == viewType) {
                    return gridLayoutManager.getSpanCount();
                } else if (ViewType.TYPE_LOADING == viewType) {
                    return gridLayoutManager.getSpanCount();
                } else if (ViewType.TYPE_ERROR == viewType) {
                    return gridLayoutManager.getSpanCount();
                } else if (ViewType.TYPE_EMPTY == viewType) {
                    return gridLayoutManager.getSpanCount();
                }
                if (spanSizeLookup != null) {
                    return spanSizeLookup.getSpanSize(position);
                }
                return 1;
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }
}
 
Example 16
Source File: HeaderAndFooterWrapperAdapter.java    From all-base-adapter with Apache License 2.0 5 votes vote down vote up
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    mInnerAdapter.onAttachedToRecyclerView(recyclerView);
    //为了兼容GridLayout
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int viewType = getItemViewType(position);
                if (mHeaderViews.get(viewType) != null) {
                    return gridLayoutManager.getSpanCount();
                } else if (mFooterViews.get(viewType) != null) {
                    return gridLayoutManager.getSpanCount();
                }
                if (spanSizeLookup != null)
                    return spanSizeLookup.getSpanSize(position);
                return 1;
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }

}
 
Example 17
Source File: HeaderRecyclerAndFooterWrapperAdapter.java    From all-base-adapter with Apache License 2.0 5 votes vote down vote up
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    mInnerAdapter.onAttachedToRecyclerView(recyclerView);
    //设置HeaderView的ViewHolder的缓存数量
    if (null != mHeaderDatas && !mHeaderDatas.isEmpty()) {
        for (HeaderData headerData : mHeaderDatas) {
            recyclerView.getRecycledViewPool().setMaxRecycledViews(headerData.getLayoutId(), headerData.getCacheSize());
        }
    }
    //为了兼容GridLayout
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int viewType = getItemViewType(position);
                if (isHeaderViewPos(position)) {
                    return gridLayoutManager.getSpanCount();
                } else if (isFooterView(viewType)) {
                    return gridLayoutManager.getSpanCount();
                }
                if (spanSizeLookup != null)
                    return spanSizeLookup.getSpanSize(position);
                return 1;
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }

}
 
Example 18
Source File: LoadMoreCommonAdapter.java    From likequanmintv with Apache License 2.0 5 votes vote down vote up
@Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView)
    {
//        Log.d("LoadMoreCommonAdapter","onAttachedToRecyclerView");
        super.onAttachedToRecyclerView(recyclerView);
        final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager)
        {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
            {
                @Override
                public int getSpanSize(int position)
                {
                    if (isShowLoadMore(position))
                    {
                        return gridLayoutManager.getSpanCount();
                    }
                    if (spanSizeLookup != null)
                    {
                        return spanSizeLookup.getSpanSize(position);
                    }
                    return 1;
                }
            });
            gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
        }
    }
 
Example 19
Source File: LatestCommentsFragment.java    From aptoide-client with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void setLayoutManager(final RecyclerView recyclerView) {
    if (appView) {
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
    } else {
        bucketSize = AptoideUtils.UI.getEditorChoiceBucketSize();
        final GridLayoutManager gridLayoutManager = new GridLayoutManager(recyclerView.getContext(), bucketSize);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {

                if (!(recyclerView.getAdapter() instanceof SpannableRecyclerAdapter)) {
                    throw new IllegalStateException("RecyclerView adapter must extend SpannableRecyclerAdapter");
                }

                int spanSize = ((SpannableRecyclerAdapter) recyclerView.getAdapter()).getSpanSize(position);
                if (spanSize >= ((GridLayoutManager) recyclerView.getLayoutManager()).getSpanCount()) {
                    return ((GridLayoutManager) recyclerView.getLayoutManager()).getSpanCount();
                } else {
                    return spanSize;
                }
            }
        });

        // we need to force the spanCount, or it will crash.
        // https://code.google.com/p/android/issues/detail?id=182400
        gridLayoutManager.setSpanCount(bucketSize);
        recyclerView.setLayoutManager(gridLayoutManager);
    }
}
 
Example 20
Source File: PinCodeRecyclerView.java    From PinCodePicker with Apache License 2.0 4 votes vote down vote up
private void initView(Context context, AttributeSet attrs) {
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PinCodeRecyclerViewWidget);
    pinCodeLength = typedArray.getInt(R.styleable.PinCodeRecyclerViewWidget_pin_code_length, 0);
    int filledOutDrawableId = typedArray.getResourceId(R.styleable.PinCodeRecyclerViewWidget_pin_code_filled_out_drawable, R.drawable.ic_pin_code_check);

    int currentAnimationResId = typedArray.getResourceId(R.styleable.PinCodeRecyclerViewWidget_pin_code_animation_current, R.animator.indicator_no_animator);
    Animator animationCurrent= AnimatorInflater.loadAnimator(context, currentAnimationResId);

    PinCodeType pinCodeType = PinCodeType.typeFromName(typedArray.getString(R.styleable.PinCodeRecyclerViewWidget_pin_code_type));

    setHasFixedSize(true);

    layoutManager = new GridLayoutManager(context, 1);
    layoutManager.setSpanCount(pinCodeLength);

    setLayoutManager(layoutManager);

    pinCodeAdapter = new PinCodeAdapter(getResources(), pinCodeLength, pinCodeType, filledOutDrawableId);
    setAdapter(pinCodeAdapter);

    pinCodeAdapter.setCurrentPinCodeAnimation(animationCurrent);

    addItemDecoration(new RecyclerViewInsetDecoration(context.getResources(), R.dimen.pin_code_view_inset_default));
    setItemAnimator(new DefaultItemAnimator());
}