Java Code Examples for android.widget.listview#getDividerHeight()

The following examples show how to use android.widget.listview#getDividerHeight() . 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: ListViewHeight.java    From MissZzzReader with Apache License 2.0 6 votes vote down vote up
/**
     * 计算listview的高度
     * @param listView
     * @return
     */
    public static int setListViewHeightBasedOnChildren(final ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return 0;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                             View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        final ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
//        MyApplication.getApplication().runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
        listView.setLayoutParams(params);
//            }
//        });

        return totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    }
 
Example 2
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 3
Source File: CommentUtil.java    From Social with Apache License 2.0 6 votes vote down vote up
/**
 * 根据item设置listview高度
 * */
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);
}
 
Example 4
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 5
Source File: ListViewUtil.java    From Social with Apache License 2.0 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);
}
 
Example 6
Source File: ViewUtils.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
/**
 * 计算ListView的高度, 重置ListView的高度.
 * 
 * @param listView
 */
public static void setListViewHeightBasedOnChildren(ListView listView)
{
	ListAdapter listAdapter = listView.getAdapter();
	if (listAdapter == null)
	{
		return;
	}

	View listItem = null;
	int totalHeight = 0;
	for (int i = 0; i < listAdapter.getCount(); i++)
	{
		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);
}
 
Example 7
Source File: ListViewUtils.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
/**
 * @param
 * @return void 返回类型
 * @Title setListViewHeightBasedOnChildren
 * @Description 根据children高度和多少,重新设置listview的高度
 */
public static void setListViewHeightBasedOnChildren(ListView listView,
                                                    int attHeight) {
    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))
            + attHeight;
    listView.setLayoutParams(params);

}
 
Example 8
Source File: ListViewUtils.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
/**
 * @param
 * @return int 返回类型
 * @Title getListViewHeightBasedOnChildren
 * @Description 得到children的高度
 */
public static int getListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return 0;
    }
    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        int itemHeight = listItem.getMeasuredHeight();
        System.out.println("itemHeight itemHeight:" + itemHeight);
        totalHeight += itemHeight;

    }

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

    return height;
}
 
Example 9
Source File: RxTool.java    From RxTools-master with Apache License 2.0 6 votes vote down vote up
/**
 * 手动计算出listView的高度,但是不再具有滚动效果
 *
 * @param listView
 */
public static void fixListViewHeight(ListView listView) {
    // 如果没有设置数据适配器,则ListView没有子项,返回。
    ListAdapter listAdapter = listView.getAdapter();
    int totalHeight = 0;
    if (listAdapter == null) {
        return;
    }
    for (int index = 0, len = listAdapter.getCount(); index < len; index++) {
        View listViewItem = listAdapter.getView(index, null, listView);
        // 计算子项View 的宽高
        listViewItem.measure(0, 0);
        // 计算所有子项的高度
        totalHeight += listViewItem.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    // listView.getDividerHeight()获取子项间分隔符的高度
    // params.height设置ListView完全显示需要的高度
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}
 
Example 10
Source File: AccountsActivity.java    From YiBo with Apache License 2.0 6 votes vote down vote up
/**
 * 设置ListView的高度,只有高度固定,才不会和外层的ScrollView产生冲突。
 * ScrollView内部嵌入ListView会引起ListView显示不全(只显示一行半)或无法滚动ListView。
 *
 * @param listView
 */
private static void setListViewHeightBasedOnChildren(ListView listView) {
       ListAdapter listAdapter = listView.getAdapter();
       if (listAdapter == null) {
           // pre-condition
           return;
       }
       if (listAdapter.getCount() == 0) {
       	listView.setVisibility(View.GONE);
       	return;
       } else {
       	listView.setVisibility(View.VISIBLE);
       }

       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);
   }
 
Example 11
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 12
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 13
Source File: MainActivity.java    From TapUnlock with Apache License 2.0 5 votes vote down vote up
public static void updateListViewHeight(ListView myListView) {
    ListAdapter myListAdapter = myListView.getAdapter();

    if (myListAdapter == null)
        return;

    // Get listView height
    int totalHeight = myListView.getPaddingTop() + myListView.getPaddingBottom();
    int adapterCount = myListAdapter.getCount();

    for (int i = 0; i < adapterCount; i++) {
        View listItem = myListAdapter.getView(i, null, myListView);

        if (listItem instanceof ViewGroup) {
            listItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                                                                ViewGroup.LayoutParams.WRAP_CONTENT));
        }

        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    // Change height of listView
    ViewGroup.LayoutParams paramsList = myListView.getLayoutParams();
    paramsList.height = totalHeight + (myListView.getDividerHeight() * (adapterCount - 1));
    myListView.setLayoutParams(paramsList);
}
 
Example 14
Source File: LetvUtil.java    From letv with Apache License 2.0 5 votes vote down vote up
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter != null) {
        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();
        }
        LayoutParams params = listView.getLayoutParams();
        params.height = (listView.getDividerHeight() * (listAdapter.getCount() - 1)) + totalHeight;
        listView.setLayoutParams(params);
    }
}
 
Example 15
Source File: AttitudeHelper.java    From Light-Android-Launcher with GNU General Public License v2.0 5 votes vote down vote up
private static int getTotalHeightOfListView(ListView listView) {
    int totalHeight = 0;
    ListAdapter adapter = listView.getAdapter();
    for (int i = 0; i < adapter.getCount() - 1; i++) {
        View view = adapter.getView(i, null, listView);
        view.measure(
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        );
        totalHeight += view.getMeasuredHeight();
    }
    return totalHeight + (listView.getDividerHeight() * (adapter.getCount()));
}
 
Example 16
Source File: NearbyRecruitFragment.java    From Social with Apache License 2.0 5 votes vote down vote up
public void setListViewHeight(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if(listAdapter == null) {
        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);
}
 
Example 17
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 18
Source File: ListViewUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 计算 ListView 高度
 * @param listView  {@link ListView}
 * @param setParams 是否 setLayoutParams
 * @return ListView 高度
 */
public static int calcListViewHeight(final ListView listView, final boolean setParams) {
    if (listView == null) return 0;
    try {
        // Adapter
        ListAdapter listAdapter = listView.getAdapter();
        // Item 总条数
        int itemCount = listAdapter.getCount();
        // 没数据则直接跳过
        if (itemCount == 0) return 0;
        // 高度
        int height = 0;
        // 获取子项间分隔符占用的高度
        int dividerHeight = listView.getDividerHeight();

        // 循环绘制每个 Item 并保存 Bitmap
        for (int i = 0; i < itemCount; i++) {
            View childView = listAdapter.getView(i, null, listView);
            WidgetUtils.measureView(childView, listView.getWidth());
            height += childView.getMeasuredHeight();
        }
        // 追加子项间分隔符占用的高度
        height += (dividerHeight * (itemCount - 1));

        // 是否需要设置高度
        if (setParams) {
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = height;
            listView.setLayoutParams(params);
        }
        return height;
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "calcListViewHeight");
    }
    return 0;
}
 
Example 19
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 20
Source File: CapturePictureUtils.java    From DevUtils with Apache License 2.0 4 votes vote down vote up
/**
 * 通过 ListView 绘制为 Bitmap
 * @param listView {@link ListView}
 * @param config   {@link Bitmap.Config}
 * @return {@link Bitmap}
 */
public static Bitmap snapshotByListView(final ListView listView, final Bitmap.Config config) {
    if (listView == null || config == null) return null;
    try {
        // Adapter
        ListAdapter listAdapter = listView.getAdapter();
        // Item 总条数
        int itemCount = listAdapter.getCount();
        // 没数据则直接跳过
        if (itemCount == 0) return null;
        // 高度
        int height = 0;
        // 获取子项间分隔符占用的高度
        int dividerHeight = listView.getDividerHeight();
        // View Bitmaps
        Bitmap[] bitmaps = new Bitmap[itemCount];

        // 循环绘制每个 Item 并保存 Bitmap
        for (int i = 0; i < itemCount; i++) {
            View childView = listAdapter.getView(i, null, listView);
            WidgetUtils.measureView(childView, listView.getWidth());
            bitmaps[i] = canvasBitmap(childView, config);
            height += childView.getMeasuredHeight();
        }
        // 追加子项间分隔符占用的高度
        height += (dividerHeight * (itemCount - 1));
        int width = listView.getMeasuredWidth();
        // 创建位图
        Bitmap bitmap = Bitmap.createBitmap(width, height, config);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(BACKGROUND_COLOR);
        // 累加高度
        int appendHeight = 0;
        for (int i = 0, len = bitmaps.length; i < len; i++) {
            Bitmap bmp = bitmaps[i];
            canvas.drawBitmap(bmp, 0, appendHeight, PAINT);
            appendHeight += (bmp.getHeight() + dividerHeight);
            // 释放资源
            bmp.recycle();
            bmp = null;
        }
        return bitmap;
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "snapshotByListView");
    }
    return null;
}