Example usage for android.widget ListView requestLayout

List of usage examples for android.widget ListView requestLayout

Introduction

In this page you can find the example usage for android.widget ListView requestLayout.

Prototype

@Override
    public void requestLayout() 

Source Link

Usage

From source file:com.lolay.android.ui.LolayBaseFragmentActivity.java

/**
 * Thanks to http://nex-otaku-en.blogspot.com/2010/12/android-put-listview-in-scrollview.html
 * WARNING: Only works with Linear Layouts or you'll get a NullPointerException in the measure code
 *///from www.ja va 2s.  c  o  m
public void listViewSetHeightFromChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }

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

From source file:com.bamobile.fdtks.util.Tools.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;/*from   w  ww  .j a  v a2 s. c o  m*/

    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

From source file:com.mobicage.rogerthat.util.ui.UIUtils.java

public static boolean setListViewHeightBasedOnItems(ListView listView, int maxHeight) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return false;
    }/*from  w ww . j av  a2 s .co m*/
    int numberOfItems = listAdapter.getCount();

    // Get total height of all items.
    int totalItemsHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
    for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
        View item = listAdapter.getView(itemPos, null, listView);
        item.measure(desiredWidth, 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;
    if (maxHeight > 0 && params.height > maxHeight) {
        params.height = maxHeight;
    }
    listView.setLayoutParams(params);
    listView.requestLayout();

    return true;
}

From source file:nodomain.freeyourgadget.gadgetbridge.adapter.GBDeviceAdapterv2.java

public void justifyListViewHeightBasedOnChildren(ListView listView) {
    ArrayAdapter adapter = (ArrayAdapter) listView.getAdapter();

    if (adapter == null) {
        return;//  w  ww  . ja  v  a  2 s .com
    }
    ViewGroup vg = listView;
    int totalHeight = 0;
    for (int i = 0; i < adapter.getCount(); i++) {
        View listItem = adapter.getView(i, null, vg);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

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

From source file:com.lgallardo.qbittorrentclient.TorrentDetailsFragment.java

/**
 * *// w ww . java 2  s  .co m
 * Method for Setting the Height of the ListView dynamically. Hack to fix
 * the issue of not showing all the items of the ListView when placed inside
 * a ScrollView
 * **
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;

    for (int i = 0; i < listAdapter.getCount(); i++) {

        long numOfLines = 1;
        view = listAdapter.getView(i, view, listView);

        if (i == 0) {
            view.setLayoutParams(new LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
        }

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);

        TextView file = (TextView) view.findViewById(R.id.file);
        TextView percentage = (TextView) view.findViewById(R.id.percentage);
        ProgressBar progressBar1 = (ProgressBar) view.findViewById(R.id.progressBar1);

        if (view.getMeasuredWidth() > desiredWidth) {

            double viewWidthLong = Double.valueOf(view.getMeasuredWidth());
            double desiredWidthLong = Double.valueOf(desiredWidth);

            numOfLines = Math.round(viewWidthLong / desiredWidthLong) + 1;

            totalHeight += (file.getMeasuredHeight() * numOfLines) + percentage.getMeasuredHeight()
                    + progressBar1.getMeasuredHeight();

        } else {
            totalHeight += view.getMeasuredHeight();
        }

    }

    LayoutParams params = listView.getLayoutParams();

    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

    listView.setLayoutParams(params);
    listView.requestLayout();

}

From source file:com.lgallardo.youtorrentcontroller.TorrentDetailsFragment.java

/**
 * *//from w w  w. j  av a2 s.  c om
 * Method for Setting the Height of the ListView dynamically. Hack to fix
 * the issue of not showing all the items of the ListView when placed inside
 * a ScrollView
 * **
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;

    for (int i = 0; i < listAdapter.getCount(); i++) {

        long numOfLines = 1;
        view = listAdapter.getView(i, view, listView);

        if (i == 0) {
            view.setLayoutParams(new LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
        }

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);

        TextView file = (TextView) view.findViewById(R.id.file);
        TextView info = (TextView) view.findViewById(R.id.info);

        if (view.getMeasuredWidth() > desiredWidth) {

            double viewWidthLong = Double.valueOf(view.getMeasuredWidth());
            double desiredWidthLong = Double.valueOf(desiredWidth);

            numOfLines = Math.round(viewWidthLong / desiredWidthLong) + 1;

            totalHeight += file.getMeasuredHeight() * numOfLines + info.getMeasuredHeight();

        } else {
            totalHeight += view.getMeasuredHeight();
        }

    }

    LayoutParams params = listView.getLayoutParams();

    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

    listView.setLayoutParams(params);
    listView.requestLayout();

}

From source file:ua.mkh.settings.full.MainActivity.java

public static void setListViewHeightBasedOnChildren(ListView lv) {
    ListAdapter listAdapter = lv.getAdapter();
    if (listAdapter == null)
        return;//from  ww  w  .j ava 2 s .  c  o m

    int desiredWidth = MeasureSpec.makeMeasureSpec(lv.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, lv);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = lv.getLayoutParams();
    params.height = totalHeight + (lv.getDividerHeight() * (listAdapter.getCount() - 1));
    lv.setLayoutParams(params);
    lv.requestLayout();
}