Skip to content Skip to sidebar Skip to footer

How To Calculate Listview Height Of Different Height Listitem In Android?

This code is work but the issue is in ListView each item of ListView is diff so last item of ListView is not display because big height ListView item get this space . here is code:

Solution 1:

your function replace by

publicstaticvoidgetTotalHeightofListView(ListView listView) {

    ListAdapter mAdapter = listView.getAdapter();

    int totalHeight = 0;
    int listWidth = listView.getMeasuredWidth();

    for (int i = 0; i < mAdapter.getCount(); i++) {
        View mView = mAdapter.getView(i, null, listView);

        mView.measure(MeasureSpec.makeMeasureSpec(listWidth, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

        totalHeight += mView.getMeasuredHeight();
        Log.w("HEIGHT" + i, String.valueOf(totalHeight));

    }

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

Solution 2:

ANS use this

mView.measure(MeasureSpec.makeMeasureSpec(listWidth, MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

on your code

mView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

Post a Comment for "How To Calculate Listview Height Of Different Height Listitem In Android?"