-5

I have a task to show fixed count of items on the screen. It doens't mean that I have fixed size of list, it means that only 5 items should be visible when scrolling.

How it can be done? I didn't find any useful information about it.

Pein
  • 1,059
  • 3
  • 10
  • 31

3 Answers3

4

I also encountered a similar problem. I have solved it almost perfectly. I chose to extend LinearLayoutManager.

public class MaxCountLayoutManager extends LinearLayoutManager {

    private int maxCount = -1;

    public MaxCountLayoutManager(Context context) {
        super(context);
    }

    public MaxCountLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public MaxCountLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    @Override
    public void setMeasuredDimension(int widthSize, int heightSize) {
        int maxHeight = getMaxHeight();
        if (maxHeight > 0 && maxHeight < heightSize) {
            super.setMeasuredDimension(widthSize, maxHeight);
        }
        else {
            super.setMeasuredDimension(widthSize, heightSize);
        }
    }

    private int getMaxHeight() {
        if (getChildCount() == 0 || maxCount <= 0) {
            return 0;
        }

        View child = getChildAt(0);
        int height = child.getHeight();
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        height += lp.topMargin + lp.bottomMargin;
        return height*maxCount+getPaddingBottom()+getPaddingTop();
    }
}

How to use:

# in kotlin
rcyclerView.layoutManager = MaxCountLayoutManager(context).apply { setMaxCount(5) }

But the height of each item needs to be the same, because I only considered the height and margin of the first item.

m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
helixs
  • 51
  • 4
  • as for me `child.layoutParams.height` returns `-2` (WRAP_CONTENT) but `child.height` returns correct value in pixels – YTerle Sep 21 '20 at 07:34
  • @YTerle yes ! You are right. The code I write is just a reference idea. You can implement it in your own way – helixs Nov 16 '20 at 11:43
3

If i am getting your question correctly, you are trying to show a fixed number of list items on the screen, whenever the user stops scrolling.

This can be done by calculating screen height/width and then setting your list item layout dimensions(height/width), accordingly.

view.getLayoutParams().width = getScreenWidth() / VIEWS_COUNT_TO_DISPLAY;

Now, depending on whether you want a horizontal or a vertical list, change width or height values of your list item layout.

Check these links

RecyclerView number of visible items

How to show exact number of items in RecyclerView?

sissyphus_69
  • 350
  • 5
  • 18
1

Simplest solution is to have onBindViewHolder() set its views height/width dynamically. For vertical list:

float containerHeight = mRecyclerView.getHeight();
holder.itemView.setMinimumHeight(Math.round(containerHeight/5));
Gotiasits
  • 1,135
  • 1
  • 10
  • 21