How to make the height of recyclerView
to wrap_content
so that there is no scrollbar shown in it.I want to make it's height to be as long as the content and i don't want the scrollbar in it, but when i try to add the contents to recyclerView
it, however, puts a scrollbar there and fixes the height of recycler view.Please help me in solving this.
Asked
Active
Viewed 1,036 times
-7
-
2It makes RecyclerView usless ... just use LinearLayout ... – Selvin Dec 05 '16 at 12:02
-
Create a layout for item and add it dyanamically. – akshay Dec 05 '16 at 12:04
-
didn't understand . Please elaborate. – Däñish Shärmà Dec 05 '16 at 12:04
-
1recylerview adds scrollbar only of the content exceeds screen height . u can use it normally. – Victor Dec 05 '16 at 12:42
-
hi @akshay can you tell me how to add items dynamically to linear layout? – pOoOf Dec 05 '16 at 12:58
-
Please refer:http://stackoverflow.com/questions/6661261/adding-content-to-a-linear-layout-dynamically – akshay Dec 05 '16 at 13:15
1 Answers
1
What you need to accomplish this is to set the height of your RecyclerView dynamically, I have previously done this but on a ListView.
If you can switch from RecyclerView to ListView you can use this method, otherwise leave a comment and I will adjust the code for your RecyclerView:
/**
* Sets ListView height dynamically based on the height of the items.
*
* @param listView to be resized
* @return true if the listView is successfully resized, false otherwise
*/
public static boolean setListViewHeightBasedOnItems(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {
int numberOfItems = listAdapter.getCount();
// Get total height of all items.
int totalItemsHeight = 0;
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = listAdapter.getView(itemPos, null, listView);
item.measure(0, 0);
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() *
(numberOfItems - 1);
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight;
listView.setLayoutParams(params);
listView.requestLayout();
return true;
} else {
return false;
}
}

Nour
- 99
- 4