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

The following are Jave code examples for showing how to use requestLayout() of the android.widget.ListView class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Forge   File: GeneratorFragment.java   View Source Code Vote up 7 votes
/**
 * Taken from Stack Overflow - https://stackoverflow.com/a/26501296
 * Updates the ListView height based on its children
 *
 * @param listView the ListView to adjust
 */

public static void setListViewHeightBasedOnChildren(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();
}
 
Example 2
Project: Viajes   File: Utils.java   View Source Code Vote up 6 votes
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);
    listView.requestLayout();
}
 
Example 3
Project: MinimalismJotter   File: ScreenUtil.java   View Source Code Vote up 6 votes
/**
 * 动态设置ListView的高度,count大于6设置固定高度
 * 在Android4.4.4版本之前,手动计算View的高度,而View中含有RelativeLayout
 * 就会出现onMeasure时NullPointerException这种错误,
 * 解决办法:
 * 一是调用measure方法前判断一下版本,
 * 二是尽量不要使用RelativeLayout,使用LinearLayout或者FrameLayout等布局。
 * @param listView
 */
public static void setListViewHeight(ListView listView) {
    int totalHeight = 0;
    if (listView == null) {
        return;
    }
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }
    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.setLayoutParams(params);
    listView.requestLayout();
}
 
Example 4
Project: underlx   File: AboutFragment.java   View Source Code Vote up 6 votes
/**** 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  ****/
private 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, WindowManager.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 5
Project: pivaa   File: AboutActivity.java   View Source Code Vote up 6 votes
/**
 * 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 6
Project: Orin   File: ViewUtil.java   View Source Code Vote up 5 votes
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 7
Project: ElephantAsia   File: ViewHelpers.java   View Source Code Vote up 5 votes
/**
 * Used when  we need to nest a ListView inside a ScrollView.
 * eg: ShowParentageFragment.
 * more info: https://stackoverflow.com/questions/27646209/disable-scrolling-for-listview-and-enable-for-whole-layout
 *
 * @param listView the listView that we want to extends
 */
public static void extendListView(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.MATCH_PARENT));
    }

    view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
    totalHeight += view.getMeasuredHeight();
  }

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

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

}
 
Example 8
Project: Orin   File: ArtistDetailActivity.java   View Source Code Vote up 4 votes
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();

    }