Line limit in Recycleview

Asked

Viewed 73 times

0

I have a RecycleView which returns many lines, configure the layout_height for a specific size, more than one cell phone to another the amount of line increases or decreases, I would like to adjust so that appear a number of lines.

ex: 4 lines, if you want to show more have the scroll bar.

2 answers

1

This happens due to different system screen sizes and densities.

In terms of usability, if you have a small and fixed number of lines (as in the case of a menu) you can use something like Linearlayout or Constraintlayout and configure it to use all the available size, then distribute the size of the items proportionally to fill the screen.

If the list needs to be dynamic (justifying the use of Recyclerview) and the size of your dataset vary, it might be better not to worry about the "perfectly fill the screen" items - even why after scrolling the top and bottom items will be partially visible. If full display of items is a requirement, a good solution can be to use libraries such as this, that will anchor the scroll of the elements.

If the previous suggestions do not solve the problem, you can dynamically calculate the height of the layout items according to the screen size. This requires obtaining the visible height of Recyclerview, subtracting margins and padding (padding) and split by the number of items to be displayed. The result will be the height of each item, which can be defined in the method onBindViewHolder of your Adapter.

A simpler solution would be to set the height of the items according to the screen dimensions. A documentation of Android Developers explains how to do this.

0

in his getItemCount try to use return Math.min(items.size(), 4);

  • Eduardo did not solve until it worsened, because the RecycleView was fixed with 4 items without scroll bar.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.