-7

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.

Swr7der
  • 849
  • 9
  • 28
pOoOf
  • 119
  • 8

1 Answers1

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