RecyclerView LinearLayoutManager set item count

2020-08-01 19:25发布

In GridLayoutManager i am able to set span count and that makes the items inside the view to resize to be able to fit that span count horizontally.

I have a LinearLayoutManager and i want to use it the same way, have a fixed number of items visible and resize them to fit.

I use both linear and grid on same view and shows items depending of the screen size. I can't seem to find a way to get both layouts showing the same amount of items.

2楼-- · 2020-08-01 19:46

Here is the kotlin version of Mihail Ignatiev's Answer

class LinearLayoutPagerManager(context: Context,orientation: Int,reverseLayout: Boolean, private val itemsPerPage: Int) : LinearLayoutManager(context,orientation,reverseLayout) {

    override fun checkLayoutParams(lp: RecyclerView.LayoutParams?): Boolean {
        return super.checkLayoutParams(lp) && lp!!.width == getItemSize()

    override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams {
        return setProperItemSize(super.generateDefaultLayoutParams())

    override fun generateLayoutParams(lp: ViewGroup.LayoutParams): RecyclerView.LayoutParams {
        return setProperItemSize(super.generateLayoutParams(lp))

    private fun setProperItemSize(lp: RecyclerView.LayoutParams): RecyclerView.LayoutParams {
        val itemSize = getItemSize()
        if (orientation == LinearLayoutManager.HORIZONTAL) {
            lp.width = itemSize
        } else {
            lp.height = itemSize
        return lp

    private fun getItemSize(): Int {
        val pageSize = if (orientation == LinearLayoutManager.HORIZONTAL) width else height
        return Math.round(pageSize.toFloat() / itemsPerPage)

3楼-- · 2020-08-01 19:52


My previous answer was to use ItemsAdapter to set each items width, from the code design stand point this is not the best solution. Proper way to do that is to extend LinearLayoutManager, since it is LayoutManager responsibility to layout item views.



//int itemsPerPage = 7;
//layoutManager = new LinearLayoutPagerManager(context, LinearLayoutManager.HORIZONTAL, false, itemsPerPage);
public class LinearLayoutPagerManager extends LinearLayoutManager {

private int mItemsPerPage;

public int getItemsPerPage()
    return mItemsPerPage;

public LinearLayoutPagerManager(Context context, int orientation, boolean reverseLayout, int itemsPerPage) {
    super(context, orientation, reverseLayout);

    mItemsPerPage = itemsPerPage;

public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {
    return super.checkLayoutParams(lp) && lp.width == getItemSize();

public RecyclerView.LayoutParams generateDefaultLayoutParams() {
    return setProperItemSize(super.generateDefaultLayoutParams());

public RecyclerView.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
    return setProperItemSize(super.generateLayoutParams(lp));

private RecyclerView.LayoutParams setProperItemSize(RecyclerView.LayoutParams lp) {
    int itemSize = getItemSize();
    if (getOrientation() == HORIZONTAL) {
        lp.width = itemSize;
    } else {
        lp.height = itemSize;
    return lp;

private int getItemSize() {
    int pageSize = getOrientation() == HORIZONTAL ? getWidth() : getHeight();
    return Math.round((float) pageSize / mItemsPerPage);



You can set exact item count in your adapter by providing a width for each item. This works only if all your items have equal width, do not forget to set


on your RecyclerView

final static int ITEMS_PER_PAGE = 7;

public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    int itemWidth = parent.getWidth() / ITEMS_PER_PAGE;

    View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.your_layout, parent, false);

    ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
    layoutParams.width = itemWidth;
    return new ItemViewHolder(itemView);
Deceive 欺骗
4楼-- · 2020-08-01 20:07

I know this was asked a while ago, but I've found a much simpler solution.

Just create your own LayoutManager and overwrite the method getChildCount()

The result should be something like this:

public int getChildCount() {
   return super.getChildCount() > 3 ? 3 : super.getChildCount();

That way, when you add this LayoutManager to the Recyclerview, it will just display 3 items at a time, no matter how many items the adapter has.

And, if you want a Pager like experience when scrolling just add a SnapHelper

SnapHelper snapHelper = new PagerSnapHelper();
登录 后发表回答