1

我需要在运行时获得准确的 listView 高度。

当我使用下面的代码时,每个 listItem 的高度都不正确。

            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();
            }
            params.height = totalHeight + listview.getDividerHeight()* (listAdapter.getCount() -1);
            listview.setLayoutParams(params);
            listview.requestLayout();

当我使用 getChild 版本时,高度是准确的,但总计数是关闭的......

        int total = 0;
        for (int i = 0; i < listview.getChildCount(); i++) {
            View childAt = listview.getChildAt(i);
            if (childAt == null)
                continue;

            int childH = childAt.getMeasuredHeight();
            total += childH;
        }
        int div = listview.getDividerHeight();
        total += (div * (listAdapter.getCount() - 1));

        if (params.height != total) {
            getLogger().info("setting measured buttons list to height: " + total);
            params.height = total;
            listview.requestLayout();
            ViewParent parent = listview.getParent();

你遇到过这个问题吗?

4

3 回答 3

9

如果你只想找到listView你可以使用的高度

listView.post(new Runnable()
{
    public void run()
    {
        listView.getHeight();
    }
});
于 2014-07-25T06:07:22.753 回答
2

listItem.measure(0, 0); 测量 0,0 的参数使用第一个参数 MeasureSpec.makeMeasureSpec(listWidth, MeasureSpec.EXACTLY) 和第二个参数 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)) 遵循此代码

公共静态无效 getTotalHeightofListView(ListView listView) {

    ListAdapter mAdapter = listView.getAdapter();

    int totalHeight = 0;
    int listWidth = listView.getMeasuredWidth();

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

        mView.measure(
                MeasureSpec.makeMeasureSpec(listWidth, MeasureSpec.EXACTLY),

                MeasureSpec.makeMeasureSpec(0, 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();

}
于 2014-07-25T05:55:18.893 回答
0

使用它,我现在正在使用它,它可以 100% 工作,我正在使用它来制作一个包含列表的滚动视图。

package com.emercs.android.easyihome.util;

 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ListAdapter;
 import android.widget.ListView;

 public class Helper {

public static void getListViewSize(ListView myListView) {
    ListAdapter myListAdapter = myListView.getAdapter();
    if (myListAdapter == null) {
        // do nothing return null
        return;
    }
    // set listAdapter in loop for getting final size
    int totalHeight = 0;
    for (int size = 0; size < myListAdapter.getCount(); size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        if (listItem != null) {
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
    }
    // setting listview item in adapter
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    if (params != null) {
        params.height = totalHeight
                + (myListView.getDividerHeight() * (myListAdapter
                        .getCount() - 1));

        myListView.setLayoutParams(params);
        // print height of adapter on log
    }
}
}

检查并添加到列表视图

   ScrollView scrollViewScenes = (ScrollView) findViewById(R.id.x);

    if (scrollViewScenes != null) {
        Helper.getListViewSize(listview);
    }

希望这可以帮助。

于 2013-08-02T07:15:05.903 回答