get AbsListView height according to every children - Android User Interface

Android examples for User Interface:ListView

Description

get AbsListView height according to every children

Demo Code


//package com.java2s;

import android.view.View;

import android.view.ViewGroup;
import android.widget.AbsListView;

import android.widget.ListAdapter;

import android.widget.RelativeLayout.LayoutParams;

public class Main {
    /**//from  w  w w.  j  a  v a2  s. c  om
     * get AbsListView height according to every children
     *
     * @param view
     * @return
     */
    public static int getAbsListViewHeightBasedOnChildren(AbsListView view) {
        ListAdapter adapter;
        if (view == null || (adapter = view.getAdapter()) == null) {
            return 0;
        }

        int height = 0;
        for (int i = 0; i < adapter.getCount(); i++) {
            View item = adapter.getView(i, null, view);
            if (item instanceof ViewGroup) {
                item.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
            }
            item.measure(0, 0);
            height += item.getMeasuredHeight();
        }
        height += view.getPaddingTop() + view.getPaddingBottom();
        return height;
    }
}

Related Tutorials