android.widget.ListView#getDividerHeight ( )源码实例Demo

下面列出了android.widget.ListView#getDividerHeight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Oy   文件: OyUtils.java
/**
 * * 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();
}
 
源代码2 项目: YiBo   文件: AccountsActivity.java
/**
 * 设置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);
   }
 
源代码3 项目: RxTools-master   文件: RxTool.java
/**
 * 手动计算出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);
}
 
源代码4 项目: MissZzzReader   文件: ListViewHeight.java
/**
     * 计算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));
    }
 
源代码5 项目: BigApp_Discuz_Android   文件: ListViewUtils.java
/**
 * @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;
}
 
源代码6 项目: BigApp_Discuz_Android   文件: ListViewUtils.java
/**
 * @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);

}
 
源代码7 项目: android-project-wo2b   文件: ViewUtils.java
/**
 * 计算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);
}
 
源代码8 项目: Social   文件: ListViewUtil.java
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);
}
 
源代码9 项目: MonsterHunter4UDatabase   文件: MHUtils.java
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();
    }
}
 
源代码10 项目: Social   文件: CommentUtil.java
/**
 * 根据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);
}
 
源代码11 项目: pivaa   文件: AboutActivity.java
/**
 * 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();

}
 
源代码12 项目: TapUnlock   文件: MainActivity.java
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);
}
 
源代码13 项目: DevUtils   文件: ListViewUtils.java
/**
 * 计算 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;
}
 
源代码14 项目: SimpleSmsRemote   文件: UIUtils.java
/**
 * 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();
}
 
源代码15 项目: Light-Android-Launcher   文件: AttitudeHelper.java
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()));
}
 
源代码16 项目: Orin   文件: ViewUtil.java
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;
        }

    }
 
源代码17 项目: letv   文件: LetvUtil.java
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);
    }
}
 
源代码18 项目: Social   文件: NearbyRecruitFragment.java
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);
}
 
源代码19 项目: DevUtils   文件: CapturePictureUtils.java
/**
 * 通过 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;
}
 
源代码20 项目: Orin   文件: ArtistDetailActivity.java
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();

    }