Skip to content Skip to sidebar Skip to footer

Listview And Recyclerview Doesnot Wrap Content With Count

this is my layout for the same. my issue is, the list view in it is not increase the height with item count. when I used wrap_content a given height like 210 pixel its shows the d

Solution 1:

This is because of your linearlayout with vertical orientation its keeping space for the lower views like radioGroups .Set height of listview dynamically use

public static void setListViewHeightBasedOnChildren(final ListView listView) {
    listView.post(new Runnable() {
        @Override
        public void run() {
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) {
                return;
            }
            int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
            int listWidth = listView.getMeasuredWidth();
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(
                        View.MeasureSpec.makeMeasureSpec(listWidth, View.MeasureSpec.EXACTLY),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));


                totalHeight += listItem.getMeasuredHeight();
                Log.d("listItemHeight " + listItem.getMeasuredHeight(), "********");
            }

            Log.d("totalHeight " + totalHeight, "********");

            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = (totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)));
            listView.setLayoutParams(params);
            listView.requestLayout();

        }
    });
}

Post a Comment for "Listview And Recyclerview Doesnot Wrap Content With Count"