how to get recycleview Item height and set as container height - android

I've a recycle view inside a fragment (max item will show in list is 5), I want to set the fragment container size according to the number of items showing in list,
What I want is user shouldn't need to scroll the list to see all the items?

A RecyclerView is intended for showing many items in a scrolling list.
That's where it gets its name from. It efficiently Recycles the Views for items that have been scrolled off screen.
From what you are describing, you are only ever showing a maximum of 5 items and you want them all on the screen at the same time, so you don't need a RecyclerView.
Just add your items to a LinearLayout and set the layoutHeight of each item as 0dp and the layoutWeight of each item as 1. That will distribute the items evenly across the full height of the LinearLayout.
Edit
If you want the fragment to adjust it's height according to the number of items, then set the height to WRAP_CONTENT for the fragment container, LinearLayout, and each of the items you add to the linear layout.

Related

Dynamic spacing between items in RecyclerView

I have horizontal RecyclerView with 10 items with square fixed size and with attached LinearSnapHelper.
I want to see an integer number of items in the list depending on the width of the RecyclerView.
Example:
I think that I have to somehow calculate the width of the RecyclerView and find how many whole items will fit into the visible part of the list, and the remaining space should be reserved for spacings between items. After that, user scrolls items with SnapHelper. Thanks in advance for help!
EDIT:
I do not want to show all the items from the list at a time, but only those that fit into the visible area of recyclerview and calculate spacings dynamically between items

Hide footer in RecyclerView when all items fit on screen

I made header and footer in my RecyclerView using typical solution with viewType in Adapter. It works fine, but I want to hide the footer if all RV items fit inside the screen, when nothing to scroll. Is there any way to know that all items will be displayed without scrolling and say to Adapter not to add footer in this case?
You can get scroll offset of recycler view with this code
recyclerView.computeVerticalScrollOffset();
Then you should check if scroll offset is more than your parent views heigh and do what you want ;)

Set the the height of all items on a row of a GridView depending on the tallest item

In an app that I'm developing I need to display a GridView with items that depending on its text (more than one line of text) may be of different height and I want to avoid that the items are shown with different heights since it's quite ugly.
When the text is only one line the GridView the items in the row are displayed properly but when the text is larger than one line they are shown like this:
How I can avoid this behavior and set the highest height of an item of a row as the height of all items of that row?

Center items in listview vertically

I have a listview, the listviews width and height should be set to match parent. So, if the listview is fully stretched and has only 2 items in it, they are at the top of the listview and on the bottom is too much useless space. I want to know, if it is possible to center these 2 items in this listview vertically, if yes, how?
List height depends on the height of the list item, that is populating the list.
If you have only two items in the list why don't you simply use a Linear Layout instead with weightSum?
Anyway, if you want to achieve the result you requested in your question you have to get the display height using method
getResources().getDisplayMetrics().heightPixels in activity context.
After that you need to set the height of the list item to the height you retrieved earlier divided by 2.
And also set gravity to center_vertical for the textviews in the list.

how to adjust ListView' height when there are ExpandableListView embedded in ListView

In an activity, there is a ListView with the related adapter. Each item in this ListView has an ExpandableListView, with related adapter.
In the beginning, there are just two items in the ListView, and all the expandable listview is not expanded. At this time, the ListView's height is not larger than the screen's height(suppose the screen's height is 800, and the ListView's height is 600. there is a blank area with height 200 below the ListView).
Then I click the expandable listview in the first item, the expandable list view is expanded. However, the ListView's height is not changed (the blank area is still there).
Is there a method to adjust the listview's height so that the blank area could be removed when I click to expand the expanable list view? Thanks.
http://www.java2s.com/Code/Android/UI/setListViewHeightBasedOnChildren.htm
This helped me out. You basically have to keep track of the layout height. And use setLayoutParams() and requestLayout() to adjust it inside your onGroupExpand and your onGroupCollapseListener methods.

Categories

Resources