Java Code Examples for android.widget.ListView#requestLayout()

The following examples show how to use android.widget.ListView#requestLayout() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AboutActivity.java    From pivaa with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Shrink listview height
 * @param listView
 * @param adapter
 */
public void setListViewHeightBasedOnChildren(ListView listView, AboutAdapter adapter) {
    int totalHeight = 0;
    for (int i = 0; i < adapter.getCount(); i++) {
        View listItem = adapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight() + 180;
        Log.i("htbridge", "listItem.getMeasuredHeight()  = " + listItem.getMeasuredHeight() );
    }

    Log.i("htbridge", "totalHeight = " + totalHeight);

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

}
 
Example 2
Source File: ShareFileFragment.java    From Cirrus_depricated with GNU General Public License v2.0 6 votes vote down vote up
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
    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();
}
 
Example 3
Source File: ListViewUtil.java    From MVPAndroidBootstrap with Apache License 2.0 6 votes vote down vote up
/**** 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 mAdapter = listView.getAdapter();

    int totalHeight = 0;

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

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

                View.MeasureSpec.makeMeasureSpec(0, View.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();
}
 
Example 4
Source File: ListViewUtil.java    From RxAndroidBootstrap with Apache License 2.0 6 votes vote down vote up
/**** 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 mAdapter = listView.getAdapter();

    int totalHeight = 0;

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

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

                View.MeasureSpec.makeMeasureSpec(0, View.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();
}
 
Example 5
Source File: OyUtils.java    From Oy with Apache License 2.0 6 votes vote down vote up
/**
 * * 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 = 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();
}
 
Example 6
Source File: MHUtils.java    From MonsterHunter4UDatabase with MIT License 6 votes vote down vote up
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

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

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    if(listView.isInLayout() == false){
        listView.requestLayout();
    }
}
 
Example 7
Source File: ViewUtil.java    From Orin with GNU General Public License v3.0 5 votes vote down vote up
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);

            int topPAdding = listView.getPaddingTop();
            int bottomPadding = listView.getPaddingBottom();

            // Set list height.
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalItemsHeight + totalDividersHeight + topPAdding + bottomPadding;
            listView.setLayoutParams(params);
            listView.requestLayout();

            return true;

        } else {
            return false;
        }

    }
 
Example 8
Source File: UIUtils.java    From faveo-helpdesk-android-app with Open Software License 3.0 5 votes vote down vote up
public static boolean setListViewHeightBasedOnItems(ListView listView) {

        DrawerItemCustomAdapter listAdapter = (DrawerItemCustomAdapter) 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;
        }

    }
 
Example 9
Source File: UIUtils.java    From SimpleSmsRemote with MIT License 5 votes vote down vote up
/**
 * Set ListView height dynamically based on the height of the items.
 *
 * @param listView to be resized
 * @see <a href="http://stackoverflow.com/questions/1778485/android-listview-display-all-available-items-without-scroll-with-static-header">stackoverflow answer</a>
 */
public static void SetListViewHeightBasedOnItems(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        throw new RuntimeException("an adapter must be set before list view can be resized");

    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);

    //get vertical padding
    int paddingVertical = listView.getPaddingTop() + listView.getPaddingBottom();

    // Set list height.
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalItemsHeight + totalDividersHeight + paddingVertical;
    listView.setLayoutParams(params);
    listView.requestLayout();
}
 
Example 10
Source File: NotificationSettingsActivity.java    From matrix-android-console with Apache License 2.0 5 votes vote down vote up
/**
 * Refresh the listview height because the listviews are displayed in a scrollview.
 * @param listView the listview.
 */
private void refreshListViewHeight(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}
 
Example 11
Source File: Utils.java    From biermacht with Apache License 2.0 5 votes vote down vote up
/**
 * This method adjusts the height of the given listView to match the combined height of all if its
 * children and the dividers between list items.  This is used to set the height of the mash step
 * list such that it does not scroll, since it is encompassed by a ScrollView.
 *
 * @param listView
 *         ListView to adjust.
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {
  ListAdapter listAdapter = listView.getAdapter();
  if (listAdapter == null) {
    return;
  }

  int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
  int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
  View view = null;
  for (int i = 0; i < listAdapter.getCount(); i++) {
    view = listAdapter.getView(i, view, listView);

    view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

    if (i == 0) {
      view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
      //totalHeight += view.getMeasuredHeight();
    }

    totalHeight += view.getMeasuredHeight();
  }
  ViewGroup.LayoutParams params = listView.getLayoutParams();
  params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
  listView.setLayoutParams(params);
  listView.requestLayout();
}
 
Example 12
Source File: PermissionsActivity.java    From APDE with GNU General Public License v2.0 5 votes vote down vote up
public void clearChecks() {
	ListView permsList = (ListView) findViewById(R.id.permissions_list);
	permsList.clearChoices();
	permsList.requestLayout();
	
	for (int i = 0; i < checked.length; i ++) {
		checked[i] = false;
	}
}
 
Example 13
Source File: ArtistDetailActivity.java    From Orin with GNU General Public License v3.0 4 votes vote down vote up
public void setHeightofListViewBasedOnContent(ListView listView) {

        ListAdapter mAdapter = listView.getAdapter();

        int totalHeight = 0;

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

            totalHeight += getResources().getDimension(R.dimen.item_list_height);
            Log.w("HEIGHT" + i, String.valueOf(totalHeight));

        }

        totalHeight = totalHeight +  (listView.getDividerHeight() * (mAdapter.getCount() - 1)) + listView.getPaddingTop();

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight;
        listView.setLayoutParams(params);
        listView.requestLayout();

    }
 
Example 14
Source File: TorrentDetailsFragment.java    From qBittorrent-Controller with MIT License 2 votes vote down vote up
/**
 * *
 * 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.name);
        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();

}